【发布时间】:2020-07-22 10:51:06
【问题描述】:
我尝试迁移 db,但出现错误,我不知道为什么。不确定什么是“格式不正确”。
//First Table
Schema::create('lkp_anime_lists', function (Blueprint $table) {
$table->id();
//more columns here
});
//Second one
Schema::create('lkp_cards', function (Blueprint $table) {
$table->id();
$table->integer('lkp_anime_list_id');
});
Schema::table('lkp_cards', function ($table) {
$table->foreign('lkp_anime_list_id')
->references('id')
->on('lkp_anime_lists')
->onDelete('cascade');
});
SQLSTATE[HY000]: 一般错误: 1005 Can't create table
anime_db.lkp_cards(errno: 150 "外键约束格式不正确") (SQL: alter tablelkp_cardsadd constraintlkp_cards_lkp_anime_list_id_foreign外键(lkp_anime_list_id)在删除级联时引用lkp_anime_lists(id)
【问题讨论】:
-
因为
lkp_cards.lkp_anime_list_id是未签名的,而lkp_anime_lists.id不是。所以要么删除->unsigned(),要么将其添加到上面的lkp_anime_lists$table->id(), -
一开始是没有的,但我还是重新测试了,我得到了同样的错误。
-
编辑您的帖子以显示两列签名匹配的代码。
-
好的,我做到了。
-
新版本的 Laravel 使
id()函数别名为bigIncrements所以你还需要将$table->integer('lkp_anime_list_id');更改为$table->bigInteger('lkp_anime_list_id');