• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot add or update a child row: a foreign key constraint fails

 
Kees deVries
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, this is what i'm trying to do.

The OrderProduct table in my database has a foreign key relationship to the ORDERID from the orders table.
pretty straight forward. But i'm getting that error. Anyone knows why?

MY create schema:




 
Brian Tkatch
Bartender
Posts: 567
25
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Please put the error message in the post. It took me a while to realize it was in the subject.

2) Try to run the statement yourself with the same values to help determine what might be failing.

3) The query is using dynamic SQL. That is generally insecure. Please consider using placeholders instead.
 
Kees deVries
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Tkatch wrote:1) Please put the error message in the post. It took me a while to realize it was in the subject.

2) Try to run the statement yourself with the same values to help determine what might be failing.

3) The query is using dynamic SQL. That is generally insecure. Please consider using placeholders instead.


I apologize.

If i try it in workbench the code does work. weird.

again the code i got in my eclipse console:

This is the errror code :Cannot add or update a child row: a foreign key constraint fails (`orderapp`.`orderproducts`, CONSTRAINT `FK_orderID` FOREIGN KEY (`order_ID`) REFERENCES `orders` (`orderID`) ON DELETE NO ACTION ON UPDATE NO ACTION)

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kees deVries wrote:This is the errror code :Cannot add or update a child row: a foreign key constraint fails (`orderapp`.`orderproducts`, CONSTRAINT `FK_orderID` FOREIGN KEY (`order_ID`) REFERENCES `orders` (`orderID`) ON DELETE NO ACTION ON UPDATE NO ACTION)

So for some reason you are violating this constraint. Which values are you trying to insert? And what's the content of the orders table?

A few other remarks regaringd your code:
1/ (as Brian already mentioned) you should consider using prepared statements in order to prevent possible SQL injection attacks (and enjoy other benefits as well). More info about prepared statements can be found here.
2/ in the update method you are not closing the Statement resource. It's always a good practice to close your resources once they are not needed anymore.

Hope it helps!
Kind regards,
Roel
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print out your INSERT statement.
Since you're not using a PreparedStatement I wouldn't be surprised if something wasn't quite right.
 
Hardik Jani
Greenhorn
Posts: 7
Android AngularJS PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is sure that you are violating reference key rule. There are two column(fields) of the table, order_id and product_id. There is possibility that there is a null value coming from your front end code function and trying to insert into your data table but you have set Not Null constraint. It is better to print or observe the value of the each field through debug, you will definitely solve your problem. 
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic