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

Foregin Key Constraint problem while loading the data #145

Open
prp-e opened this issue Sep 13, 2023 · 1 comment
Open

Foregin Key Constraint problem while loading the data #145

prp-e opened this issue Sep 13, 2023 · 1 comment

Comments

@prp-e
Copy link

prp-e commented Sep 13, 2023

Greetings.
I'm using rails 7.0.4 and I accidentally removed my production sqlite3 file. Since I used yaml_db in order to keep a dump for my database, I reminded of the file and tried to recover it.
For some obvious reasons, we've decided to move our code base to postgresql and when I set everything up, it works fine. So, when I run rails db:data:load command, I get this error:

rails aborted!
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "credit_packs" violates foreign key constraint "fk_rails_21f35c4567"
DETAIL:  Key (user_id)=(1) is not present in table "users".


Caused by:
PG::ForeignKeyViolation: ERROR:  insert or update on table "credit_packs" violates foreign key constraint "fk_rails_21f35c4567"
DETAIL:  Key (user_id)=(1) is not present in table "users".

Tasks: TOP => db:data:load
(See full trace by running task with --trace)

What should I do now?

Explanation about the credit_pack model : It's something which every user has. So I don't know why this dataloader thingy tries to first create them then users.

@chriscz
Copy link

chriscz commented Aug 19, 2024

You probably figured something out by now. I haven't looked much further, but it seems this might do the job: https://stackoverflow.com/questions/38112379/disable-postgresql-foreign-key-checks-for-migrations

To quote a top stackoverflow answer by andro83:

For migration, it is easier to disable all triggers with:

SET session_replication_role = 'replica';

And after migration reenable all with

SET session_replication_role = 'origin';

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

2 participants