Laravel 7迁移错误:外键约束格式错误

时间:2020-07-27 09:31:50

标签: laravel

我正在运行此迁移:

Schema::create('logs', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->string('key');
        $table->text('value')->nullable();
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });

“用户”表迁移(在Laravel 5.8迁移)

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('last_message_id')->nullable();
        $table->timestamps();
    });

我收到的错误:

Foreign key constraint is incorrectly formed

1 个答案:

答案 0 :(得分:0)

在我看来,您似乎使用laravel的先前版本进行了用户迁移。使用db工具(例如phpmyadmin,Sequel等)查看一下users.id字段的实际数据类型是什么。

我认为它不是bigInteger,所以您的解决方案可以是只使用

$table->unsignedInteger('user_id');