Expected at most one result but got ...

I'm not sure if I've encountered a bug or not. I have the following action:
read :get do
argument :id, :uuid
get? true
end
read :get do
argument :id, :uuid
get? true
end
and the following associated code interface:
define :get do
args [:id]
end
define :get do
args [:id]
end
I expect it to return at most one result. In iex, if I call the following:
iex(11)> Flight.get("a8c463a0-2ae0-4f04-8fbe-7bb1abe94ff6", tenant: "8f251331-d27b-4d42-a05b-5dbe3e019ebe")
iex(11)> Flight.get("a8c463a0-2ae0-4f04-8fbe-7bb1abe94ff6", tenant: "8f251331-d27b-4d42-a05b-5dbe3e019ebe")
I receive the following error:
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.MultipleResults{
count: 5,
.....
}
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.MultipleResults{
count: 5,
.....
}
I confirmed that my record exists and that there is only one in my DB. More interestingly, here's the query that's being performed:
[debug] QUERY OK source="flights" db=11.9ms queue=0.1ms idle=283.7ms
SELECT f0."id", f0."number", f0."departure_time", f0."arrival_time", f0."airline_id", f0."departure", f0."arrival", f0."alternate", f0."user_id", f0."plane_id" FROM "flights" AS f0 WHERE (f0."airline_id"::uuid = $1::uuid) ["8f251331-d27b-4d42-a05b-5dbe3e019ebe"]
[debug] QUERY OK source="flights" db=11.9ms queue=0.1ms idle=283.7ms
SELECT f0."id", f0."number", f0."departure_time", f0."arrival_time", f0."airline_id", f0."departure", f0."arrival", f0."alternate", f0."user_id", f0."plane_id" FROM "flights" AS f0 WHERE (f0."airline_id"::uuid = $1::uuid) ["8f251331-d27b-4d42-a05b-5dbe3e019ebe"]
I only see a check for the tenant here (airline_id) and not the id of the flight. Am I missing something super obvious in my resource or is this a bug? Thanks a lot it in advance! 😀
3 Replies
ZachDaniel
ZachDaniel2y ago
adding the argument doesn't apply a filter to the read action You likely want something like this in the read action:
filter expr(id == ^arg(:id))
filter expr(id == ^arg(:id))
richaard0
richaard0OP2y ago
Of course 🤦🏼‍♂️ thanks!
Alan Heywood
Alan Heywood2y ago
@richaard0 you can also do the following, which will define the filter on the read action for you, and return a single record instead of a list:
code_interface do
...
define :get, action: :read, get_by: [:id]
end
code_interface do
...
define :get, action: :read, get_by: [:id]
end

Did you find this page helpful?