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

时间:2015-06-11 04:18:09

标签: php mysql laravel-4

我对这个错误/错误感到疯狂。我正在使用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日志,则会插入行。那么,发生了什么?

1 个答案:

答案 0 :(得分:0)

如果使用mySQL客户端一切正常,你应该看看你的SQL语句和Laravel之间有什么区别。

$queries = DB::getQueryLog();
$last_query = end($queries);

检查Laravel执行的内容。

相关问题