Ash FrameworkAF
Ash Frameworkโ€ข3y agoโ€ข
9 replies
Dirigible

Deleting resources that have existing relationships

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 CarLogs 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 CarLogs behind?

However, when I try to delete a Car with existing CarLogs 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?
Was this page helpful?