[英]Laravel MySQL Cannot insert null in nullable foreign key
我對這個錯誤/錯誤感到瘋狂。 我正在使用Laravel 4.2開發一個應用程序,其中我有一個像這樣創建的表:
Schema::create('stock_items', function(Blueprint $table){
....
$table->integer('color_id')->unsigned()->nullable();
$table->integer('size_id')->unsigned()->nullable();
....
$table->foreign('color_id')->references('id')->on('colors');
$table->foreign('size_id')->references('id')->on('sizes');
});
兩列都可以為null
但是當我插入這樣的內容時:
$stock_item->color_id = NULL;
$stock_item->size_id = NULL;
我收到MySQL錯誤告訴我: Could not insert: Cannot add or update a child row: a foreign key constraint fails
但是如果我在任何MySQL客戶端中運行完全相同的查詢laravel日志,則會插入該行。 發生什么了??
如果使用mySQL客戶端一切正常,你應該看到你的SQL語句和Laravel之間有什么區別。
$queries = DB::getQueryLog();
$last_query = end($queries);
檢查Laravel執行的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.