UT测试报错

来源:8-7 商品编辑之后端开发上

慕仰5218754

2019-05-24

org.springframework.dao.DataIntegrityViolationException:

Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT fk_product_procate FOREIGN KEY (product_category_id) REFERENCES tb_product_category (product_category_id))

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

The error occurred while setting parameters

SQL: UPDATE tb_product SET product_name=?, product_category_id=? WHERE product_id = ? AND shop_id=?

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT fk_product_procate FOREIGN KEY (product_category_id) REFERENCES tb_product_category (product_category_id))

; SQL []; Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT fk_product_procate FOREIGN KEY (product_category_id) REFERENCES tb_product_category (product_category_id)); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT fk_product_procate FOREIGN KEY (product_category_id) REFERENCES tb_product_category (product_category_id))

写回答

2回答

慕沐1441344

2021-05-01

mark

0
0

翔仔

2019-05-25

同学好,错误非常明显,你的tb_product里面的product_category_id并没有在tb_product_category表里创建。。违反了主外键的约束。。

0
2
翔仔
回复
慕仰5218754
不是建表,是说里面的表的数据,比如你tb_product插入了product_category_id为1,而tb_product_category里面的product_category_id为1的数据不存在,就会报这个错。程序说1是1,不会骗人的,同学其实可以直接把错误贴到百度,就会发现这个是违反数据库完整性的问题。
2019-05-27
共2条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程