sarah
sarah

Reputation: 2397

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

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

Upvotes: 6

Views: 4789

Answers (1)

MrGomez
MrGomez

Reputation: 23886

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

Upvotes: 4

Related Questions