Casecade delete an associated resource from AshArchival

Hello everyone
I have a question.
I am using AshArchival.
Let’s say I have a diary and a photo in a 1: N relationship.
I want to record the archived_at of the photo when the diary is deleted.
I wrote the code below but it doesn’t work, can you give me any advice on this?


postgres do
    table "dental_diaries"
    repo Dentallog.Repo

    references do
      reference :photos, on_delete: :delete, name: "fk_diary_photos"

I thought that if I write it as the code above, it would behave as I thought it would.

1 Like

right, so the references there is configuring the underlying database’s deletion behavior, not Ash’s behavior.

You will want to use the archive_related option provided by ash_archival. In your diaries resource, set

archival do
  archive_related :photos

A few things to keep in mind:

  1. right now, each photo will be archived one at a time. Now that bulk updates are implemented in ash core, we can update that behavior. It is just a matter of time, but at the moment it will be iterative (i.e if there are 1000 photos in a diary, it will be 1000 updates)

  2. make sure that your photos resource has a destroy action and that it also uses ash_archival (otherwise the archive_related will actually just destroy them)

1 Like

Thank you for your response.

1 Like