为什么在Laravel 5.8中创建外键失败?

Ift*_*din 7 mysql laravel eloquent

下面的迁移脚本在旧版本的Laravel中运行平稳,但是我将其添加到了新的Laravel 5.8中并运行了该脚本。我越来越Error: foreign key was not formed correctly

评估迁移:

public function up() { 
    Schema::create('evaluation', function (Blueprint $table) { 
        $table->increments('id'); 
        $table->integer('user_id')->unsigned()->index(); 
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}
Run Code Online (Sandbox Code Playgroud)

用户迁移:

public function up() { 
    Schema::create('users', function (Blueprint $table) { 
        $table->bigIncrements('id'); 
        $table->timestamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

Bil*_*win 17

正如我们在上面的评论中讨论的那样,外键列必须与其引用的主键具有相同的数据类型。

你宣布你的user.id主键$table->bigIncrements('id')成为BIGINT UNSIGNED AUTO_INCREMENT在MySQL语法。

您必须声明$table->unsignedBigInteger('user_id')BIGINT UNSIGNED在MySQL中使用的外键,使其与user.id列的外键兼容。

  • 啊哈!`unsignedBigInteger`,而不仅仅是`bigInteger`! (2认同)