Dirigible
Dirigible
AEAsh Elixir
Created by Dirigible on 8/8/2023 in #support
Filter by struct keys in a read action
So I have an ash actions. It receives a struct as an input. I want to create a filter that filters by values inside the struct. here's a simplified version of the action:
read :get_corresponding_price do
argument :instance, :struct do
allow_nil? false
contraints [instance_of: Instance]
end

get? true

filter expr(instance_type == ^arg(:instance).machine_type)
end
read :get_corresponding_price do
argument :instance, :struct do
allow_nil? false
contraints [instance_of: Instance]
end

get? true

filter expr(instance_type == ^arg(:instance).machine_type)
end
I get an error like this:
== Compilation error in file lib/my_project/cloud/instance_price.ex ==
** (KeyError) key :machine_type not found in: {:_arg, :instance}. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
lib/my_project/cloud/instance_price.ex:146: (module)
== Compilation error in file lib/my_project/cloud/instance_price.ex ==
** (KeyError) key :machine_type not found in: {:_arg, :instance}. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
lib/my_project/cloud/instance_price.ex:146: (module)
Is there a way to get the key out within a filter like this, or will I need to filter the data in a different way? Is the only way of doing this through a preparation?
6 replies
AEAsh Elixir
Created by Dirigible on 6/7/2023 in #support
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?
10 replies
AEAsh Elixir
Created by Dirigible on 2/26/2023 in #support
How to Delete Resource?
I have a resource that has attributes and relationships to other resources. I want to delete it completely, how would I do that?
6 replies