我有product_variants表,它与另外两个表--数据、products和variants --有关
当我试图更新我的产品时,它会返回这个错误。
“字段列表”(SQL: update
product_variantssetid= 0118d682-970f-4859-b30b-e1c79df9c342,parent_id= ?,name= Interface,slug=接口,photo= ?,type= textarea,active= yes,created_at= 2020-08-05 14:57:42,updated_at= 2020-08-05 14:57:42,d14=2020-08-05 14:57:42,d14="variant_id":"0118d682-970f-4859-b30b-e1c79df9c342"},children = {"id":"7efe338d-9026-4fe8-b16f-6b99bc51871a","parent_id":"0118d682-970f-4859-b30b-e1c79df9c342","name":"1x RJ45,1x SD (XC/HC)读卡器,1x (4K @60 HC) HDMI,1x Mini,2x Type-A USB3.2 Gen1,1x Type-A USB3.2 Gen2,1x Type-C (USB3.2 Gen2 / DP),1x类型-C USB3.2 Gen2x2",“段塞”:“1x RJ45,1x SD (XC/HC)读卡器,1x (4K @60 HC) HDMI,1x微型显示端口,2x类型-A USB3.2 Gen1,1x类型-A USB3.2 Gen2,1x Type-C (USB3.2 Gen2 / DP),1x类型-C USB3.2 Gen2x2",”照片“:空”类型“:”文本区域“,”活动“:”是“,"created_at":"2020-08-05 14:57:42","updated_at":"2020-08-05 14:57:42"}其中product_id = 4ea5f41f-aa2f-46d4-b033-e4e875cc8ff4,variant_id (0))
代码
product_variants迁移
public function up()
{
Schema::create('product_variants', function (Blueprint $table) {
$table->primary(['product_id', 'variant_id']);
$table->uuid('product_id')->nullable();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->uuid('variant_id')->nullable();
$table->foreign('variant_id')->references('id')->on('variants');
});
}product model
public function variations(){
return $this->belongsToMany(Variant::class, 'product_variants', 'product_id', 'variant_id');
}variant model
public function products(){
return $this->belongsToMany(Product::class, 'product_variants', 'variant_id', 'product_id');
}知道是什么原因导致了这个问题吗?
发布于 2020-08-10 05:26:46
我建议创建自己的枢轴模型并坦率地设置主键:
use Illuminate\Database\Eloquent\Relations\Pivot;
class ProductVariants extends Pivot
{
protected $primaryKey = ['product_id', 'variant_id'];
public $incrementing = false;
}在此之后,您应该修改您的关系:
public function variations()
{
return $this->belongsToMany(Variant::class)
->using(ProductVariants::class);
}https://stackoverflow.com/questions/63334416
复制相似问题