下面的迁移脚本在旧版本的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');
});
}
用户迁移:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
答案 0 :(得分:6)
正如我们在上面的评论中讨论的那样,外键列必须与其引用的主键具有相同的数据类型。
您将user.id
主键声明为$table->bigIncrements('id')
,在MySQL语法中它变成了BIGINT UNSIGNED AUTO_INCREMENT
。
您必须将外键声明为$table->unsignedBigInteger('user_id')
,在MySQL中将变为BIGINT UNSIGNED
,使其与user.id
列的外键兼容。
答案 1 :(得分:0)
update your `integer('user_id')` to `bigInteger('user_id')`
public function up() {
Schema::create('evaluation', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}