dependent: :delete_all only deletes direct children
class Tag
has_closure_tree dependent: :delete_all
end
With the code above, @tag.destroy only deletes @tag's direct children. Its grandchildren are all orphaned, keeping old parent_ids which no longer exist. However, the hierarchy records of all direct children and grandchildren do get deleted though. Is this intended?
Thanks.
Foreign keys shouldn't ever point to deleted records (and fk constraints should prevent that from happening). It should be a recursive delete.
Thanks for reporting this. PRs are welcome!
Yeah, I'm surprised this can actually happen. Searched in the codebase a little bit, the :dependent option is simply only being passed into ActiveRecord, closure_tree doesn't seem to have any particular handling about destroying.
If someone needs this and wants to take a swing at it, you'll need at least 3 steps:
- capture all descendant IDs (either in a temp table or in memory) via the
hierarchy_table -
DELETEall the references from thehierarchy_table -
DELETEfrom the model tableWHERE id IN (all descendant IDs)