Ash.Resource.Change.CascadeDestroy (ash v3.4.68)
View SourceCascade a resource's destroy action to a related resource's destroy action.
If after_action? is true this change adds an after-action hook that explicitly calls destroy on any records related via the named relationship. It will optimise for bulk destroys where possible. This makes it safe to use in atomic actions, but might not be possible depending on the data layer setup (see warning below).
If after_action? is false this change will add a before-action hook for relationships where the child record points to the parent (has_many, has_one, many_to_many).
Beware database constraints
Think carefully before using this change with data layers which enforce referential integrity (ie PostgreSQL and SQLite) and you may need to defer constraints for the relationship in question.
See also:
Cascading notifications
By default notifications are disabled for the related destroy. This is to avoid potentially sending a lot of notifications for high-cardinality relationships.
Options
:relationship
(atom/0
) - Required. The name of the relationship to work on:action
(atom/0
) - The name of the destroy action to call on the related resource. Uses the primary destroy by default.:read_action
(atom/0
) - The name of the read action to call on the related resource to find results to be destroyed:return_notifications?
(boolean/0
) - Return notifications for all destroyed records? The default value isfalse
.:after_action?
(boolean/0
) - If true all the cascade destroys are done in after_action hooks. This makes it safe to use in atomic actions The default value istrue
.
Example
change {Ash.Resource.Change.CascadeDestroy, relationship: :comments, action: :destroy}
or, equivalently using Ash.Resource.Change.Builtins.cascade_destroy/2
:
change cascade_destroy(:comments, action: :destroy)