迁移:引用同一表的两个外键导致错误

时间:2019-04-06 14:58:07

标签: laravel migration laravel-5.7

我试图建立一个博客,但由于一个表的迁移错误而被封锁。

我试图更改外键的创建顺序,

仅将两个外键与数组一起构建,

$table->foreign(['commentary_id', 'post_id'])->references(['id', 'post_id'])->on('commentaries');

评论迁移

$table->integer('post_id')->unsigned();
$table->integer('id')->unsigned();
$table->string('content');
$table->integer('user_id')->unsigned();
$table->timestamps();

$table->primary(['post_id', 'id']);
$table->foreign('post_id')->references('id')->on('posts');
$table->foreign('user_id')->references('id')->on('users');

响应迁移

$table->integer('post_id')->unsigned();
$table->integer('commentary_id')->unsigned();
$table->integer('id')->unsigned();
$table->string('content');
$table->integer('user_id')->unsigned();
$table->timestamps();

$table->primary(['post_id', 'commentary_id', 'id']);
$table->foreign(['commentary_id', 'post_id'])->references(['id', 'post_id'])->on('commentaries');
$table->foreign('user_id')->references('id')->on('users');

还有我得到的错误:

General error: 1005 Can't create table `blog-lurius`.`responses` 
(errno: 150 "Foreign key constraint is incorrectly formed") 
(SQL: alter table `responses` add constraint `responses_commentary_id_post_id_foreign` foreign key (`commentary_id`, `post_id`) references `commentaries` (`id`, `post_id`))

我希望在迁移过程中不会出现致命错误。我的两个外键都有效的地方。

0 个答案:

没有答案