Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

hibernate

i am having a user table which has desname as FK referring to des table ,i am trying to add desname in user but i am gettng Cannot add or update a child row: a foreign key constraint fails error.

desname is prepopulated and i am selected the same for he user.Where i am doing wrong I ma using mysql and hibernate hbm

like image 393
sarah Avatar asked Apr 14 '10 10:04

sarah


1 Answers

The most common occurrence of this error is failing to create (and store) the required instance of the entity referenced by the foreign key. This generally occurs when the create operation for the parent entity is omitted, out of order, or in cases where the parent isn't correctly stored in the database prior to attempting creation of your weak entity.

For the record, this specific error message is only emitted when Hibernate attempts to write to the (MySQL) database it's connected to. Example code would significantly help here for solving your individual problem, as pointed out in the comments.

For everyone else interested, SO offers plenty of information for this particular error: https://stackoverflow.com/search?q=Hibernate+foreign+key+constraint+fails

like image 195
MrGomez Avatar answered Oct 18 '22 20:10

MrGomez