View Source Ash.Resource.Change.CascadeDestroy (ash v3.2.6)

Cascade a resource's destroy action to a related resource's destroy action.

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.

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:

  1. postgres.references.reference.deferrable DSL
  2. sqlite.references.reference.deferrable DSL
  3. PostgreSQL's SET CONSTRAINTS documentation
  4. SQLite's PRAGMA defer_foreign_keys documentation

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 The default value is :destroy.

  • :return_notifications? (boolean/0) - Return notifications for all destroyed records? The default value is false.

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)