下面的迁移脚本在旧版本的 Laravel 中运行顺利,但我将它添加到我的新 Laravel 5.8 中并运行了脚本。我收到 错误:外键格式不正确
评估迁移:
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();
});
}
最佳答案
正如我们在上面的评论中所讨论的,外键列必须与其引用的主键具有相同的数据类型。
您将 user.id
主键声明为 $table->bigIncrements('id')
在 MySQL 中变为 BIGINT UNSIGNED AUTO_INCREMENT
语法。
您必须将外键声明为 $table->unsignedBigInteger('user_id')
,这将在 MySQL 中变为 BIGINT UNSIGNED
,使其与外键兼容到 user.id
列。
关于mysql - 为什么在 Laravel 5.8 中创建外键失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54990828/