【发布时间】:2019-05-10 18:41:47
【问题描述】:
我有 2 个模型:产品和类别,其中每个产品属于一个类别(或没有)
然后我在产品表中放了一个“category_id”。但是 category_id 可以为空。
在 Laravel 中:
Schema::create('products', function (Blueprint $table) {
$table->unsignedInteger('category_id')->default(0);
$table->foreign('category_id')->references('id')->on('categories');
但是当我需要将类别更改为 NULL (0) 时,我得到错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
我该如何解决?
最好的问候
【问题讨论】:
-
我认为这是一个副本:stackoverflow.com/questions/37735055/…