evilmartians / evil-seed

A Gem for creating partial anonymized dumps of your database using your app model relations.
MIT License
447 stars 19 forks source link

Foreign key nullification #11

Closed masterzeus05 closed 2 years ago

masterzeus05 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.