我正在为这个错误/漏洞而疯狂。我正在使用 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 错误提示:
无法插入:无法添加或更新子行:违反外键约束条件
但是,如果我在任何 MySQL 客户端中运行完全相同的查询,该行将被插入。那么,到底是怎么回事呢?
(SQL: insert into `model_items_stock` (`model_item_id`, `color_option_id`, `size_option_id`, `updated_at`, `created_at`) values (1, NULL, NULL, 2015-06-11 21:03:23, 2015-06-11 21:03:23))
我运行了它,它可以正常工作。 - Javier Enríquez