运行迁移时,最后一个表以标题中的错误结束。
我的数据库是本地 MySQL。
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
shopping_list_develop
。#sql-1698_2b
(errno: 150 "外键约束形成不正确") (SQL: alter tableproducts
add constraintproducts_shopping_list_id_foreign
外键 (shopping_list_id
) 引用shopping_lists
(id
) on delete set null on update cascade)
这是我检查的内容:
父表 ( shopping_lists
) 在子表 ( products
)之前创建。
外键shopping_list_id
与它引用的列的类型相同。
这两个表,shopping_lists
并products
具有相同的数据库引擎(InnoDB)。
我已阅读其他答案,但找不到解决方案。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Carbon\Carbon;
class CreateShoppingListsTable extends Migration {
public function up()
{
Schema::create('shopping_lists', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable()->default(Carbon::now()->toDateString());
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::table('shopping_lists', function(Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::dropIfExist('shopping_lists');
}
}
皈依舞