请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

添加商品DAO层测试的时候调试失败。

这是批量添加图片的测试,不太懂怎么操作,而后面的那个productDao测试也发生了类似的错误,好像都是数据的异常
org.springframework.dao.DataIntegrityViolationException:

Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (020.tb_product_img, CONSTRAINT fk_proimg_product FOREIGN KEY (product_id) REFERENCES tb_product (product_id))

The error may exist in file [E:\works\o2o\target\classes\mapper\ProductImgDao.xml]

The error may involve com.imooc.o2o.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 (020.tb_product_img, CONSTRAINT fk_proimg_product FOREIGN KEY (product_id) REFERENCES tb_product (product_id))

; Cannot add or update a child row: a foreign key constraint fails (020.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 (020.tb_product_img, CONSTRAINT fk_proimg_product FOREIGN KEY (product_id) REFERENCES tb_product (product_id))
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:246)

正在回答 回答被采纳积分+3

2回答

翔仔 2020-11-04 00:15:29

 Cannot add or update a child row: a foreign key constraint fails (020.tb_product_img, CONSTRAINT fk_proimg_product FOREIGN KEY (product_id) REFERENCES tb_product (product_id))

说的是同学的productImg里面的productId在tb_product里面不存在,也就是没法分辨图片是属于哪个商品的,需要你添加对应的product_id并且product_id在tb_product存在才可以

0 回复 有任何疑惑可以回复我~
提问者 韬1430337 2020-11-03 12:24:08

### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`020`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))

### The error may exist in file [E:\works\o2o\target\classes\mapper\ProductDao.xml]

### The error may involve com.imooc.o2o.dao.ProductDao.insertProduct-Inline

### The error occurred while setting parameters

### SQL: INSERT INTO   tb_product(product_name,product_desc,img_addr,   normal_price,promotion_price,priority,create_time,   last_edit_time,enable_status,product_category_id,   shop_id)   VALUES   (?,?,?,   ?,?,?,?,   ?,?,?,   ?)

### Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`020`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))

; Cannot add or update a child row: a foreign key constraint fails (`020`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`)); nested exception is java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`020`.`tb_product`, CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`))

这个是商品添加调试的信息,控制台并没有报错

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信