繁体   English   中英

Laravel MySQL无法在可以为空的外键中插入null

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM