yamldb / yaml_db

Rails plugin for a database-independent dump format, data.yml .......... Seeking new maintainers! See https://github.com/yamldb/yaml_db/issues/143
https://rubygems.org/gems/yaml_db
949 stars 199 forks source link

Foregin Key Constraint problem while loading the data #145

Open prp-e opened 1 year ago

prp-e commented 1 year ago

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 commented 4 weeks ago

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';