You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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)
The text was updated successfully, but these errors were encountered: