添加商品DAO层测试的时候调试失败。
来源:8-1 商品添加之Dao层的实现
韬1430337
2020-11-03
这是批量添加图片的测试,不太懂怎么操作,而后面的那个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)
2回答
-
翔仔
2020-11-04
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存在才可以
00 -
韬1430337
提问者
2020-11-03
### 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`))
这个是商品添加调试的信息,控制台并没有报错
00
相似问题