完整性约束 violation/foreign 键约束失败
Integrity constraint violation/foreign key constraint fails
我有以下完整性约束违规,其中外键约束失败。任何想法表示赞赏。
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(go-motoring
.promotion_products
, CONSTRAINT
promotion_products_product_id_foreign
FOREIGN KEY (product_id
)
REFERENCES products
(id
) ON DELETE CASCADE) (SQL: insert into
promotion_products
(product_id
, promotion_id
) values (0, 3))
产品型号
public function categories()
{
return $this->belongsToMany(EcommerceCategory::class, 'product_categories',
'product_id', 'ecommerce_category_id');
}
推广模式
public function products()
{
return $this->belongsToMany(Product::class, 'promotion_products',
'promotion_id', 'product_id');
}
促销控制器
$params = $request->except(['_token', 'select_product']);
$promotion = new Promotion($params);
$promotion->save();
//category sync
$data['product_ids'] = $request->select_product; //convert product ids in array
$promotion->products()->sync($data['product_ids']); //sync (insert & update)
solution. because 0 value exist in incoming data. 0 not save as a foreign key
我有以下完整性约束违规,其中外键约束失败。任何想法表示赞赏。
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
go-motoring
.promotion_products
, CONSTRAINTpromotion_products_product_id_foreign
FOREIGN KEY (product_id
) REFERENCESproducts
(id
) ON DELETE CASCADE) (SQL: insert intopromotion_products
(product_id
,promotion_id
) values (0, 3))
产品型号
public function categories()
{
return $this->belongsToMany(EcommerceCategory::class, 'product_categories',
'product_id', 'ecommerce_category_id');
}
推广模式
public function products()
{
return $this->belongsToMany(Product::class, 'promotion_products',
'promotion_id', 'product_id');
}
促销控制器
$params = $request->except(['_token', 'select_product']);
$promotion = new Promotion($params);
$promotion->save();
//category sync
$data['product_ids'] = $request->select_product; //convert product ids in array
$promotion->products()->sync($data['product_ids']); //sync (insert & update)
solution. because 0 value exist in incoming data. 0 not save as a foreign key