亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

errno: 150 “外鍵約束的格式不正確”但檢查我知道的一切

errno: 150 “外鍵約束的格式不正確”但檢查我知道的一切

PHP
慕娘9325324 2021-06-16 14:17:12
問題運行遷移時,最后一個表以標題中的錯誤結束。我的數據庫是本地 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();


查看完整回答
反對 回復 2021-06-19
  • 1 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號