Merhaba veri tabanımda 4 adet tablo oluşturmaya çalışıyorum users tweets tags ve tweet_tag adlı pivot tablo
paylaşılan tweetlere etkiket sistemi yapmaya çalışıyorum pivot tablo yu öğrenmek için ancak php artisan migrate dediğim zaman hata alıyorum ve bir türlü anlamadım foreign edilen sutunların data type ları aynı ve oluşturmayı sıra ile oluşturmayıda denedim ilk users table sonra tweets tags tweet_tag sırası gibi ama genede yaramadı sorun ne anlamadım.sanırım çok ufak bir şeyi kaçırıyorum.
create_tags_table:
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
//pivot
Schema::create('tweet_tag', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('tweet_id');
$table->unsignedBigInteger('tag_id');
$table->timestamps();
$table->foreign('tweet_id')->references('id')->on('tweets')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
}
create_tweets_table:
public function up()
{
Schema::create('tweets', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->text('content');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
create_users_table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('password');
$table->timestamps();
});
}
tweets teki foreign de bir sorun yok ama bu iki satırdan birini ekleyip php artisan migrate dediğimde hata alıyorum
$table->foreign('tweet_id')->references('id')->on('tweets')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
Hata:
SQLSTATE[HY000]: General error: 1005 Can't create tablecms.tweet_tag(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tabletweet_tagadd constrainttweet_tag_tweet_id_foreignforeign key (tweet_id) referencestweets(id) on delete cascade)