問題運行遷移時,最后一個表以標題中的錯誤結束。我的數據庫是本地 MySQL。錯誤Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table shopping_list_develop。#sql-1698_2b(errno: 150 "外鍵約束形成不正確") (SQL: alter table productsadd 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)。我已閱讀其他答案,但找不到解決方案。遷移2019_05_13_192170_create_shopping_lists_table.php<?phpuse 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'); }}
1 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
在您的2019_05_13_192700_create_products_table.php
遷移中, onDelete 您將值設置為 null。
$table->foreign('shopping_list_id')->references('id')->on('shopping_lists') ->onDelete('set null') ->onUpdate('cascade');
但是您聲明的列不可為空
$table->unsignedBigInteger('shopping_list_id');
嘗試通過執行以下操作使該列可以為空:
$table->unsignedBigInteger('shopping_list_id')->nullable();
- 1 回答
- 0 關注
- 162 瀏覽
添加回答
舉報
0/150
提交
取消