这个错误/错误让我发疯了。我正在使用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);
检查拉拉维尔执行了什么。