From bfefef7fd580d607a3360c8e34800168ced9410c Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Thu, 15 Dec 2022 17:16:53 -0500 Subject: [PATCH] improvement: distinct on source of query, not relationship destination --- lib/join.ex | 2 +- test/sort_test.exs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/join.ex b/lib/join.ex index 5a50798..30c5f92 100644 --- a/lib/join.ex +++ b/lib/join.ex @@ -313,7 +313,7 @@ defmodule AshPostgres.Join do joined_query else from(row in joined_query, - distinct: ^Ash.Resource.Info.primary_key(relationship.destination) + distinct: ^Ash.Resource.Info.primary_key(joined_query.__ash_bindings__.resource) ) end else diff --git a/test/sort_test.exs b/test/sort_test.exs index 93c9176..00a297f 100644 --- a/test/sort_test.exs +++ b/test/sort_test.exs @@ -159,6 +159,20 @@ defmodule AshPostgres.SortTest do |> Ash.Query.sort(title: :desc) |> Ash.Query.filter(linked_posts.title in ["aaa", "bbb", "ccc"] or title == "aaa") ) + + assert [ + %{title: "ccc"}, + %{title: "bbb"}, + %{title: "aaa"} + ] = + Api.read!( + Post + |> Ash.Query.sort(title: :desc) + |> Ash.Query.filter( + linked_posts.title in ["aaa", "bbb", "ccc"] or + post_links.source_post_id == ^post2.id + ) + ) end test "calculations can be sorted on w/o loading aggregates they reference" do