我有这个迁移:
文章
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
$table->string('slug')->unique();
$table->text('body');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
对于类别
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
当我尝试运行命令php artisan migrate
时,出现错误:
General error: 1215 Cannot add foreign key constraint (SQL: alter table`articles` add constraint articles_category_id_foreign foreign key (`category_id`) references `categories` (`id`) on delete cascade)
多次查看我发现的所有问题。