user20230119

user20230119

Cascade delete on recursive relationship

I have a recursive resource related to itself.

I can destroy a record with reference :parent, index?: true, on_delete: :delete and all nested children are deleted by PostgreSQL.

I later found cascade_destroy. What would it do differently? Would it run the destroy action for all nested children records? Is it useful for soft destroys?

    destroy :destroy do
      primary? true
      change cascade_destroy(:children, action: :destroy)
    end

I tried removing on_delete: :delete and using it, but it didn’t work?

Do I need on_delete: :delete or deferrable: true to use cascade_destroy?

{:error,
 %Ash.Error.Invalid{
   errors: [
     %Ash.Error.Changes.InvalidAttribute{
       field: :id,
       message: "would leave records behind",
       private_vars: [
         constraint: "entities_parent_id_fkey",
         constraint_type: :foreign_key,
         detail: "Key (id)=(02df79f3-8b30-4288-bb50-ac446cc97f41) is still referenced from table \"entities\"."
       ],
       value: nil,
       splode: Ash.Error,
       bread_crumbs: [],
       vars: [],
       path: [],
       stacktrace: #Splode.Stacktrace<>,
       class: :invalid
     }
   ]
 }}

Most Liked

sevenseacat

sevenseacat

Author of Ash Framework

The docs for cascade_destroy explain this - it’s useful when you need to run business logic on the records being destroyed, it will run a destroy action for each of the records being destroyed, so you can hook in your own logic.

If a database on delete will do what you need, use that instead as it will be much faster!

If you want to run cascade_destroy as an after_action (the default) then you’ll need deferrable: :initially on the database reference, otherwise you get errors like the one you’ve seen. Alternatively, you can use a before_action (cascade_destroy(:stuff, after_action?: false)) and you won’t need to change the foreign key.

zachdaniel

zachdaniel

Creator of Ash

Yes, it would. Although assuming the actions can be done in bulk, N will be “depth” not “number of records * depth”

Where Next?

Popular in Questions Top

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement