外键约束问题
来源:8-1 商品添加之Dao层的实现
初级Java开发
2019-01-08
老师,单元测试productDao和productImgDao时都报了这个错误,但是我查看了数据库,这个外键是成功的,不知道要怎么解决?
org.springframework.dao.DataIntegrityViolationException:
Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o
.tb_product_img
, CONSTRAINT fk_proimg_product
FOREIGN KEY (product_id
) REFERENCES tb_product
(product_id
))
The error may involve com.imooc.myo2o.dao.ProductImgDao.batchInsertProductImg-Inline
The error occurred while setting parameters
SQL: insert into tb_product_img(img_addr,img_desc,priority,create_time,product_id) values ( ?,?,?,?,? ) , ( ?,?,?,?,? )
Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o
.tb_product_img
, CONSTRAINT fk_proimg_product
FOREIGN KEY (product_id
) REFERENCES tb_product
(product_id
))
; SQL []; Cannot add or update a child row: a foreign key constraint fails (o2o
.tb_product_img
, CONSTRAINT fk_proimg_product
FOREIGN KEY (product_id
) REFERENCES tb_product
(product_id
)); nested exception is java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o
.tb_product_img
, CONSTRAINT fk_proimg_product
FOREIGN KEY (product_id
) REFERENCES tb_product
(product_id
))
写回答
2回答
-
同学好,这个问题比较明显,说的就是往tb_product_img插入的数据里没有product_id或者product_id不在tb_product里存在,需要先在那边新建
212019-09-08 -
初级Java开发
提问者
2019-01-08
已解决。
112019-09-15
相似问题