Skip to content

Sorting by a calculation that performs a subquery raises an error #262

@sevenseacat

Description

@sevenseacat

eg.

Artist resource has a calculation followed?, defined like:

calculate :followed?, :boolean, expr(exists(artist_followers, follower_id == ^actor(:id)))

Attempting to sort a query reading artists by this calculation, like:

read :search do
  prepare fn query, _context ->
    Ash.Query.sort(query, followed?: :desc)
  end
end

Raises the following error:

[error] ** (ArgumentError) subqueries are not allowed in `order_by` expressions
    (ecto 3.11.2) lib/ecto/query/builder/dynamic.ex:72: Ecto.Query.Builder.Dynamic.partially_expand/5
    (ecto 3.11.2) lib/ecto/query/builder/order_by.ex:163: Ecto.Query.Builder.OrderBy.dynamic_or_field!/4
    (ecto 3.11.2) lib/ecto/query/builder/order_by.ex:142: anonymous fn/4 in Ecto.Query.Builder.OrderBy.order_by_or_distinct!/4
    (elixir 1.16.2) lib/enum.ex:1826: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (ecto 3.11.2) lib/ecto/query/builder/order_by.ex:140: Ecto.Query.Builder.OrderBy.order_by_or_distinct!/4
    (ecto 3.11.2) lib/ecto/query/builder/order_by.ex:157: Ecto.Query.Builder.OrderBy.order_by!/5
    (ash_sql 0.1.1-rc.12) lib/sort.ex:222: AshSql.Sort.sort/6
    ...

Notes

  • if there is an exists in an order_by then we need to move the calculation into the select and then sort on the selected name

Runtime

  • Elixir version 1.16.2-otp-26
  • Erlang version 26.2.3
  • Ash version 3.0.0-rc.31
  • AshPostgres version 2.0.0-rc.11
  • PostgreSQL version 15.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions