Skip to content

jechol/ash_sql_load_sort_page_bug

Repository files navigation

Ash SQL Load + Sort + Page Bug

Problem Overview

This repository reproduces a bug in Ash Framework / AshSQL.

Issue: Query fails when combining:

  • load (aggregate)
  • sort (by calculation using has_one relationship)
  • page

The bug only occurs when all three are present together.

Resource Setup

# 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

Reproducing the Issue

Failing Case

# 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!()

Working Cases

# 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!()

Running Tests

MIX_ENV=test mix ecto.reset
mix test

Test Results

  • error: load + sort + page: FAILURE
  • ok: load + sort: SUCCESS
  • ok: load + page: SUCCESS
  • ok: sort + page: SUCCESS

File Structure

  • lib/my_domain/chat.ex: Chat resource with has_one, calculation, and aggregate
  • lib/my_domain/message.ex: Message resource
  • test/my_domain/chat_test.exs: Bug reproduction tests

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages