【发布时间】:2020-07-23 06:21:33
【问题描述】:
你好,我有两张桌子:
产品:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title');
...
$table->boolean('slider_mode')->default(false);
});
}
和滑块:
public function up()
{
Schema::create('slider', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
...
$table->integer('products_id')->unsigned()->index();
$table->foreign('products_id')->references('id')->on('products')->onDelete('cascade');
});
问题是当我使用滑块创建产品表时,产品的 id 没有在 products_id 中引用
这是我的控制器(存储功能):
...
if($x1['slider_mode'] == 1){ # x1 is $request->all();
Slider::create($x1);
}
Product::create($x1);
return redirect('admin/products');
当我创建两者时,products_id 是否应该不使用产品 ID 自动完成?
但我遇到了错误:
一般错误:1364 字段“products_id”没有默认值
【问题讨论】:
-
为什么不简单地将 product_id 设为 nullable ,如果你不存储它会创建滑块