I have two tables with each a foreign key referencing the other:
users
favorite_post_id int NOT NULL
posts
user_id int NOT NULL
Consider a user can have many posts, but only a single favorite post.
I want to seed my database with two CSV files using the COPY command. The data contain rows that reference each other (i.e. a post with (id: 2, user_id: 1), and a user with (id: 1, favorite_post_id: 2))
However I'm unable to get the insertion to happen simultaneously, resulting in an error of inserting to one table violating the foreign constraint to the other.
insert or update on table "posts" violates foreign key constraint "FK_d8feca7198a0931f8234dcc58d7"
Key (user_id)=(1) is not present in table "users".
Is there a way to commit the insertion at once, so that it happens simultaneously?
users.post_idis nullable. In that case, load the data without settingusers.post_idand then updateusersto setpost_idafter thepoststable is loaded.favorite_post_idis not nullable in the actual scenario. Consider the user has to mark a favorite post upon registering. A bit silly, I should definitely consider a better example.