fix: load by __order__ ascending

This commit is contained in:
Zach Daniel 2023-02-07 17:43:53 -05:00
parent 627151afc1
commit b41d383b47
3 changed files with 61 additions and 3 deletions

View file

@ -585,7 +585,7 @@ defmodule AshPostgres.DataLayer do
from(row in subquery(query_with_order),
select: row,
order_by: row.__order__
order_by: [asc: row.__order__]
)
else
order_by = %{query.windows[:order] | expr: query.windows[:order].expr[:order_by]}
@ -818,7 +818,7 @@ defmodule AshPostgres.DataLayer do
where: field(source, ^source_attribute) in ^source_values,
inner_lateral_join: destination in ^subquery,
on: field(source, ^source_attribute) == field(destination, ^destination_attribute),
order_by: destination.__order__,
order_by: [asc: destination.__order__],
select: destination,
distinct: true
)}
@ -926,7 +926,7 @@ defmodule AshPostgres.DataLayer do
where: field(source, ^source_attribute) in ^source_values,
inner_lateral_join: destination in ^subquery,
select: destination,
order_by: destination.__order__,
order_by: [asc: destination.__order__],
distinct: true
)}
else

View file

@ -281,6 +281,60 @@ defmodule AshPostgres.AggregateTest do
|> Ash.Query.sort(:first_comment)
|> Api.read_one!()
end
test "it can be sorted on and produces the appropriate order" do
post1 =
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "b"})
|> Ash.Changeset.manage_relationship(:post, post1, type: :append_and_remove)
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "c"})
|> Ash.Changeset.manage_relationship(:post, post1, type: :append_and_remove)
|> Api.create!()
post2 =
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "a"})
|> Ash.Changeset.manage_relationship(:post, post2, type: :append_and_remove)
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "b"})
|> Ash.Changeset.manage_relationship(:post, post2, type: :append_and_remove)
|> Api.create!()
post3 =
Post
|> Ash.Changeset.new(%{title: "title"})
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "c"})
|> Ash.Changeset.manage_relationship(:post, post3, type: :append_and_remove)
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "d"})
|> Ash.Changeset.manage_relationship(:post, post3, type: :append_and_remove)
|> Api.create!()
assert [%{last_comment: "d"}, %{last_comment: "c"}] =
Post
|> Ash.Query.load(:last_comment)
|> Ash.Query.sort(last_comment: :desc)
|> Ash.Query.limit(2)
|> Api.read!()
end
end
test "sum aggregates show the same value with filters on the sum vs filters on relationships" do

View file

@ -179,6 +179,10 @@ defmodule AshPostgres.Test.Post do
sort(title: :asc_nils_last)
end
first :last_comment, :comments, :title do
sort(title: :desc)
end
max(:highest_comment_rating, [:comments, :ratings], :score)
min(:lowest_comment_rating, [:comments, :ratings], :score)
avg(:avg_comment_rating, [:comments, :ratings], :score)