Deleting resources that have existing relationships

Version info:
* ash 2.9.11 4baa454f
* ash_postgres 1.3.28 aa32bf2b
* 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
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
...
** (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?
6 Replies
Dirigible
DirigibleOP•2y ago
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
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
ZachDaniel•2y ago
You probably want on_delete: :nilify 🙂
Dirigible
DirigibleOP•2y ago
Wow and just like that it works! Thanks @Zach Daniel!
ZachDaniel
ZachDaniel•2y ago
My pleasure 😄
Dirigible
DirigibleOP•2y ago
How do I mark as solved?
ZachDaniel
ZachDaniel•2y ago
already done 😆 but you'd right click the channel in the left and add the solved tag and then close it kind of inconvenient. I'd like to make our bot do it, where if your post is tagged with solved it gets closed automatically

Did you find this page helpful?