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