2

I'm trying to order by a string provided dynamically. The field I want to order is alias in the select.

query
|> select([t], %{orders_count: fragment("? AS orders_count", count(t.id)})
|> order_by(fragment("?", ^field))

This will generate this, and will not work.

order_by: [asc: fragment("?", ^"orders_count")],

But if I put the string name without a variable

query
|> select([t], %{orders_count: fragment("? AS orders_count", count(t.id)})
|> order_by(fragment("orders_count"))

will generate this, and it works.

 order_by: [asc: fragment("orders_count")],

But I can't understand why it doesn't work with the first example.

David Magalhães
  • 750
  • 4
  • 10
  • 27

0 Answers0