• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:




 
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
Android PHP AngularJS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  
 
reply
    Bookmark Topic Watch Topic
  • New Topic