文章迁移
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('people_id');
$table->string('title');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('people');
});
人员迁移
Schema::create('people', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('twitter');
$table->timestamps();
});
错误
SQLSTATE[HY000]: General error: 1005 Can't create table `article`.`#sql-1204_c` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tab
le `articles` add constraint `articles_author_id_foreign` foreign key (`author_id`) references `people` (`id`))
我不知道我做错了什么,两个实体对我来说都是一致的,我不知道为什么我收到错误150
答案 0 :(得分:1)
首先通过 -
制作people_id
unsigned
$table->integer('people_id')->unsigned();
更改此行 -
$table->foreign('author_id')->references('id')->on('people');
对此 -
$table->foreign('people_id')->references('id')->on('people');
检查migrations
的订单。首先运行people
表迁移,然后运行articles
表迁移。
答案 1 :(得分:0)
这是因为你还没有定义author_id字段
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('author_id');
$table->string('title');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('people');
});