外键约束的格式不正确,Laravel

时间:2020-04-09 18:13:26

标签: laravel laravel-7

我尝试迁移数据库,但是出现错误,我不确定为什么。不知道什么是“格式不正确”。

//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无法创建表anime_dblkp_cards(错误号:150“外键约束格式不正确”)(SQL:更改表{{1} }在删除级联上添加约束lkp_cards外键(lkp_cards_lkp_anime_list_id_foreign)引用lkp_anime_list_idlkp_anime_lists

2 个答案:

答案 0 :(得分:3)

您应该使用

$table->unsignedBigInteger('lkp_anime_list_id')

相反,因为主键和外键应该使用相同的数据类型

答案 1 :(得分:0)

这对我有用

$table->bigInteger('lkp_anime_list_id')->unsigned();

适用于Laravel 6及更高版本