Я хочу запустить миграцию под названием articles
, которая выглядит следующим образом:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->refrence('id')->on('users')->onDelete('cascade');
$table->string('title');
$table->string('slug');
$table->text('body');
$table->text('description');
$table->text('body');
$table->string('imageUrl');
$table->string('tags');
$table->integer('viewCount')->default(0);
$table->integer('commentCount')->default(0);
$table->timestamps();
});
}
Но я получаю эту ошибку:
SQLSTATE[HY000]: General error: 1005 Can't create table `gooyanet`.`#sql-1ce8_1d` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `articles` add constraint `articles_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
Итак, я искал в сети, и они говорят, что я должен сначала создать таблицы, а затем добавить внешние ключи, поэтому вместо этого я написал это:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('slug');
$table->text('description');
$table->text('body');
$table->string('imageUrl');
$table->string('tags');
$table->integer('viewCount')->default(0);
$table->integer('commentCount')->default(0);
$table->timestamps();
});
Schema::table('articles', function($table)
{
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
}
Но теперь ошибка такая:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'gooyanet.articles' doesn't exist (SQL: alter table `articles` add constraint `articles_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
Итак, что мне делать, чтобы запустить эту миграцию с внешним ключом?