[英]cannot alter a table to add foreign key?
嘗試在項目表的“member_id”列上添加外鍵,該表引用成員表上的主鍵“id”。
項目遷移
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('title');
$table->text('description');
$table->timestamps();
});
會員遷移
Schema::create('members', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('country_id');
$table->string('name');
$table->string('email');
$table->integer('active');
$table->timestamps();
});
AddMemberIdToProjects 遷移
Schema::table('projects', function (Blueprint $table) {
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('id')->on('members');
});
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
使其unsigned
和nullable
。
分開創建列和添加外鍵邏輯,避免類似錯誤:
Schema::table('projects', function (Blueprint $table) {
$table->unsignedBigInteger('member_id')->nullable();
});
Schema::table('projects', function (Blueprint $table) {
$table->foreign('member_id')->references('id')->on('members');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.