Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Foreign Key Constraint Fails

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting "SQLException: Cannot add or update a child row: a foreign key constrant fails". And I have no clue why. I am using Hibernate3. If anyone can glance at this and let me know if they see anything that stands out, it would be much appreciated. Thanks. Below are relavent files:









This is the SQL that is used to create the database:



And this is the test code used to add a new record:

 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you turn on logging and get the SQL statement that is being sent to the database? Which database, because you could probebly try running the exact statement directly to the database as gett a more specific reason, depending on database.

Mark
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
Can you turn on logging and get the SQL statement that is being sent to the database? Which database, because you could probebly try running the exact statement directly to the database as gett a more specific reason, depending on database.

Mark



This is the statement:

SQL [insert into t_item (title, description, image_id, type_id, paypal) values (?, ?, ?, ?, ?)]

I'll try running it against the database directly and see what happens.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to run it against MySQL directly gives me the exact same error message.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, everyone can slap me in the head. It was failing because I was inserting a value for image_id and the t_image table didn't have any data yet. Ugh! It's been a long week. Thanks for the help.
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
Ok, everyone can slap me in the head. It was failing because I was inserting a value for image_id and the t_image table didn't have any data yet. Ugh! It's been a long week. Thanks for the help.



I like the SQL statement with the ? marks, I meant the actual SQL statement executed. But anyway, I was leaning towards the Image, but I think the key is how you define certain relationships. Isn't there a difference between composite and association relationships, and what would automatically create records in the child tables for you if it doesn't exist. Kind of like walking the relationships.

Anyway, congrats for the head butt.

Mark
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic