商品添加报错,调试信息如下,翔仔麻烦看一下

来源:8-6 商品添加之前端实现下

慕勒4553688

2020-04-24

{success=false, errMsg=com.imooc.o2o.exceptions.ProductOperationException: 创建商品失败: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 dsf FOREIGN KEY (product_category_id) REFERENCES tb_shop_category (shop_category_id))

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: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT dsf FOREIGN KEY (product_category_id) REFERENCES tb_shop_category (shop_category_id))

; SQL []; Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT dsf FOREIGN KEY (product_category_id) REFERENCES tb_shop_category (shop_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 dsf FOREIGN KEY (product_category_id) REFERENCES tb_shop_category (shop_category_id))}

写回答

2回答

翔仔

2020-04-25

同学好,

Cannot add or update a child row: a foreign key constraint fails (o2o.tb_product, CONSTRAINT dsf FOREIGN KEY (product_category_id) REFERENCES tb_shop_category (shop_category_id))

你这个tb_product表里面的product_category_id为什么要去关联tb_shop_category里面的shop_category_id?应该是tb_product_category里面的product_category_id才对吧。。而且 你得确保tb_product里面得product_category_id在tb_product_category里面存在

0
1
慕勒4553688
非常感谢!
2020-04-26
共1条回复

weixin_慕瓜9035920

2020-04-24

应该是product_category_id不对,你填一个你数据库表中有的product_category_id试试。

1
2
慕勒4553688
2020-04-24 13:19:36.672 [http-nio-8080-exec-10] DEBUG com.imooc.o2o.dao.ProductDao.insertProduct - ==> Preparing: 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 (?,?,?, ?,?,?,?, ?,?,?, ?) 2020-04-24 13:19:36.781 [http-nio-8080-exec-10] DEBUG com.imooc.o2o.dao.ProductDao.insertProduct - ==> Parameters: 测试商品(String), 测试商品(String), \upload\images\item\shop\1\2058920200424131922.jpg(String), 50(String), 55(String), 6(Integer), 2020-04-24 13:18:51.251(Timestamp), 2020-04-24 13:19:16.248(Timestamp), 1(Integer), 4(Long), 1(Long) 4月 24, 2020 1:19:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 4月 24, 2020 1:19:43 下午 org.springframework.jdbc.support.SQLErrorCodesFactory 信息: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
2020-04-24
共2条回复

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

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

5127 学习 · 8148 问题

查看课程