jaynetics / delete_recursively

A Ruby gem that adds a better option to recursively delete dependent ActiveRecords.
MIT License
20 stars 2 forks source link

Does it work with Foreign Key database constraints? #4

Open iamonkara opened 1 year ago

iamonkara commented 1 year ago

image

I get these errors on a has_one and belongs_to association

/home/onkara/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.7.2/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_params': ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_5676777bf1" on table "settings" (PG::ForeignKeyViolation)
jaynetics commented 1 year ago

hi!

this gem does not take special care of DB constraints.

it basically just piggy-backs on active_records dependent: option and triggers at the same time as e.g. dependent: :destroy.

as a result, it should only run into trouble in the same scenarios where a (full chain of) dependent: :destroy callbacks would also run into trouble, if i'm not mistaken.

its a bit hard to tell what's going wrong without knowing the exact setup of relations and full stacktrace.

edit: actually what i said above is not universally true because delete_recursively deletes stuff in the relation tree depth-first after the first record is deleted, whereas dependent: :destroy proceeds breadth-first, so it might make a difference when it comes to constraints and perhaps this "traversal strategy" should be configurable.