Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Insert Statement Order (in conjunction with joined inheritance) #1988

Open
stfnsche opened this issue Nov 3, 2023 · 0 comments
Open

Comments

@stfnsche
Copy link

stfnsche commented Nov 3, 2023

The following DB structure leads to a foreign key constraint violation due to incorrect statement order.

Class/Table Base
Class/Table Experiment extends Base (JOINED inheritance)
Class/Table Measurement extends Base (JOINED inheritance) with a bidirectional ManyToOne relation to Experiment.

creating an experiment with a measurement and than saving it leads to a foreign key constraint violation

the statements are:
INSERT INTO base (id, created_at, last_modified_at, type) VALUES (?, ?, ?, ?)
bind => [100, 2023-11-03T10:21:00.943585800, 2023-11-03T10:21:00.943585800, Experiment]
INSERT INTO base (id, created_at, last_modified_at, type) VALUES (?, ?, ?, ?)
bind => [101, 2023-11-03T10:21:00.945157200, 2023-11-03T10:21:00.945157200, Measurement]
INSERT INTO measurements (value, experiment, id) VALUES (?, ?, ?)
bind => [0.2, 100, 101]

The error occurs because experiment has a foreign key on table experiments(id) which has not been inserted yet. Before creating the first measurement the experiment insert statement should be issued first. This (IMHO correct) statement order is issued when using hibernate

Hibernate: insert into base (created_at,last_modified_at,type,id) values (?,?,'Experiment',?)
Hibernate: insert into experiments (description,id) values (?,?)
Hibernate: insert into base (created_at,last_modified_at,type,id) values (?,?,'Measurement',?)
Hibernate: insert into measurements (experiment,value,id) values (?,?,?)

A demo project where you can test this behaviour can be found here
https://bitbucket.org/sscheiber/persistence-arena
(calling docker/setup.bat or docker/setup.sh provides you with a preconfigured postgres DB to run the unit tests against)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant