{@thread.name}

Dirigible
2023-06-07

Dirigible:

Version info:

* ash 2.9.11  4baa454f
* ash_postgres 1.3.28 aa32bf2b

So I have a Ash Postgres resource, lets call it Car . I also have another Ash Postgres resource called CarLog . Whenever a change happens to Car a CarLog with the nature of the change is created. (so 1:many relation between Car : CarLog ) However if a resource instance of Car is deleted I don’t want to delete and of the CarLog s associated with it.

In my CarLog resource I have:

postgres do
    table "car_logs"
    repo MyProject.Repo

    references do
      reference :car, on_delete: :nothing
    end
  end

To my understanding this should make it possible to destroy a Car resource instance, and leave the associated CarLog s behind?

However, when I try to delete a Car with existing CarLog s I’m getting this error:

     ** (Ash.Error.Invalid) Input Invalid
     
     * Invalid value provided for id: would leave records behind.
     
     nil
     
       (elixir 1.14.3) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
       (ash_postgres 1.3.28) lib/data_layer.ex:1196: AshPostgres.DataLayer.handle_errors/1
       (ash_postgres 1.3.28) lib/data_layer.ex:1719: AshPostgres.DataLayer.destroy/2
       (ash 2.9.11) lib/ash/actions/destroy.ex:314: anonymous fn/6 in Ash.Actions.Destroy.as_requests/5
       (ash 2.9.11) lib/ash/changeset/changeset.ex:2001: Ash.Changeset.run_around_actions/2
...

I’ve found the source of the error in ash_postgres , and seems to because it doesn’t meet a foreign key constraint: https://github.com/ash-project/ash_postgres/blob/0ad06c6b636c4b4d3ff66971ddde68828d446667/lib/data_layer.ex#L1484-L1485

What do I need to do to allow the destruction of resources instances that have references to others?

Dirigible:

In case you need it here is the constraint specified in the migration:

alter table(:car_logs) do
      modify :car_id,
             references(:car,
               column: :id,
               name: "car_logs_car_id_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :nothing
             )
    end

zachdaniel:

You probably want on_delete: :nilify 🙂

Dirigible:

Wow and just like that it works! Thanks <@197905764424089601>!

zachdaniel:

My pleasure 😄

Dirigible:

How do I mark as solved?

zachdaniel:

already done 😆

zachdaniel:

but you’d right click the channel in the left and add the solved tag

zachdaniel:

and then close it

zachdaniel:

kind of inconvenient. I’d like to make our bot do it, where if your post is tagged with solved it gets closed automatically