Sorting on the first relationship in an aggregate

I want to do an aggregate over a m2m table where the order is important, so I write the code below but whatever I do I can't seem to sort on the first table. Should I be able to do that?
aggregates do
list :phone_numbers_list, [:some_many_to_one_table, :user], :phone_number do
public? true
sort expr(some_many_to_many_table.order)
end
end
aggregates do
list :phone_numbers_list, [:some_many_to_one_table, :user], :phone_number do
public? true
sort expr(some_many_to_many_table.order)
end
end
Solution:
tldr: Calculations ❤️ The solution was to use a calculation on :some_many_to_one_table that got the phone number: ```elixir...
Jump to solution
5 Replies
Solution
Andreas Ekeroot @ work
tldr: Calculations ❤️ The solution was to use a calculation on :some_many_to_one_table that got the phone number:
calculations do
calculate :phone_number, :string, expr(some_many_to_one_table.phone_number), public?: true
end
calculations do
calculate :phone_number, :string, expr(some_many_to_one_table.phone_number), public?: true
end
And then create another calculation on the first resource:
calculate :phone_numbers_list,
{:array, :string},
expr(list(some_many_to_one_table, query: [sort: :order], field: :phone_number))
calculate :phone_numbers_list,
{:array, :string},
expr(list(some_many_to_one_table, query: [sort: :order], field: :phone_number))
And then I could load everything and produce a super pretty query with the best type annotation.
Ash.load!(clock, :phone_numbers_list, actor: context.actor) |> IO.inspect()
Ash.load!(clock, :phone_numbers_list, actor: context.actor) |> IO.inspect()
$1::text[])::text[]::text[]
$1::text[])::text[]::text[]
ZachDaniel
ZachDaniel3d ago
Yeah I thought we fixed the double type casting but must not have
Andreas Ekeroot @ work
Oh, does double type casting do anything? Except for making me happy. 🙂
ZachDaniel
ZachDaniel3d ago
double type casting can have performance impacts where it gets confused about the index to use I think? Either way it would probably end up as something like:
$1::text[])::text[]
$1::text[])::text[]
Andreas Ekeroot @ work
Got it! It's always the risk of making the query planner confused that makes me worried when interacting with databases.

Did you find this page helpful?