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

Foreign key nullification #11

Closed
masterzeus05 opened this issue Mar 10, 2022 · 3 comments
Closed

Foreign key nullification #11

masterzeus05 opened this issue Mar 10, 2022 · 3 comments

Comments

@masterzeus05
Copy link

masterzeus05 commented Mar 10, 2022

Hey! Sorry to bother again, but this time I found an issue that I don't know if it is really a bug or just something about my schema or configuration.

For example, I have 3 models, Person, Car, PersonCarBorrow, and the PersonCarBorrow has defined:

belongs_to :person
belongs_to :car

And when dumping this schema, with root on Person, the PersonCarBorrow will have the Person foreign key person_id NULL, and the Car foreign key with the correct value.

My gems versions:

activerecord (= 5.2.6)
evil-seed (0.2.0)

After a little search, I think the issue is related with this line relation_dumper#149, as the foreign_key is nullified because of the condition on excluded = ... || reflection.name == inverse_reflection.
A fork of this gem already made a change related to this shippio/evil-seed-41025192b46649cf43af91d7fd7a80358a9e7379.

Thank you for your help.

@Envek
Copy link
Member

Envek commented Mar 10, 2022

I believe that reason for this is avoiding errors with foreign key constraints in the database. So it actually may be a feature (but not sure).

Please provide your EvilSeed configuration.

Possible workarounds: instruct evil-seed to dump Person, Cars, and PersonCarBorrows separately.

@masterzeus05
Copy link
Author

masterzeus05 commented Mar 10, 2022

The configuration is something like

EvilSeed.configure do |config|
  config.root("Person", id: ids) do |root|
    root.exclude(/\.translations/)
  end

  config.root("Car") do |root|
    root.exclude(/\.translations/)
    root.exclude(/car\.person_car_borrows\.person\..*/)
  end
end

So the problem might actually be related to this exclude root.exclude(/card\.person_car_borrows\.person\..*/)?
The only reason I do that exclude is that it is not of my interest to export all "Persons" that a Car is borrowed to, but now that I'm looking at it, maybe the exclude should be root.exclude(/card\..*/) to avoid the PersonCarBorrows altogether from the Cars?

EDIT: Just to add that Person has defined in it

has_many :person_car_borrows, dependent: :restrict_with_exception

EDIT2: Ok I think that the problem is just related with the order of the configuration? It just worked now by changing the order, so thanks!

@Envek
Copy link
Member

Envek commented Mar 10, 2022

Got it. This is feature to keep referential integrity.

If you don't interested in persons that a Car is borrowed to, then you won't have them in the database. In that case, if you try to insert record to person_car_borrows with identifier that is missing from persons (and you have foreign key in place) then database will throw an error that this value violates referential integrity. So it is better to nullify foreign key value if we know that we're not going to reference anything.

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