This repository reproduces a bug in Ash Framework / AshSQL.
Issue: Query fails when combining:
load(aggregate)sort(by calculation usinghas_onerelationship)page
The bug only occurs when all three are present together.
# Chat has a has_one relationship with sorting
has_one :last_message, MyDomain.Message do
from_many? true
sort sent_at: :desc
end
# Calculation using the has_one relationship
calculate :last_message_sent_at, :utc_datetime, expr(last_message.sent_at)
# Aggregate
count :message_count, :messages# load + sort + page = FAILURE
Chat
|> Ash.Query.for_read(:read)
|> Ash.Query.load([:message_count])
|> Ash.Query.sort(last_message_sent_at: :desc)
|> Ash.Query.page(limit: 10)
|> Ash.read!()# load + sort (no page) = SUCCESS
Chat
|> Ash.Query.for_read(:read)
|> Ash.Query.load([:message_count])
|> Ash.Query.sort(last_message_sent_at: :desc)
|> Ash.read!()
# load + page (no sort) = SUCCESS
Chat
|> Ash.Query.for_read(:read)
|> Ash.Query.load([:message_count])
|> Ash.Query.page(limit: 10)
|> Ash.read!()
# sort + page (no load) = SUCCESS
Chat
|> Ash.Query.for_read(:read)
|> Ash.Query.sort(last_message_sent_at: :desc)
|> Ash.Query.page(limit: 10)
|> Ash.read!()MIX_ENV=test mix ecto.reset
mix test- ❌
error: load + sort + page: FAILURE - ✅
ok: load + sort: SUCCESS - ✅
ok: load + page: SUCCESS - ✅
ok: sort + page: SUCCESS
lib/my_domain/chat.ex: Chat resource with has_one, calculation, and aggregatelib/my_domain/message.ex: Message resourcetest/my_domain/chat_test.exs: Bug reproduction tests