diff --git a/test/filter_test.exs b/test/filter_test.exs index 5492864b..69ab990c 100644 --- a/test/filter_test.exs +++ b/test/filter_test.exs @@ -1151,6 +1151,43 @@ defmodule AshPostgres.FilterTest do |> length == 1 end + test "using exists with has_many with limit" do + comment = + Comment + |> Ash.Changeset.for_create(:create, %{title: "test"}) + |> Ash.create!() + + for score <- 1..5 do + Ash.Changeset.for_create(AshPostgres.Test.Rating, :create, %{ + score: score, + resource_id: comment.id + }) + |> Ash.create!(context: %{data_layer: %{table: "comment_ratings"}}) + end + + # The top_ratings relationship has limit: 2 and sort: [score: :desc] + # So it should only include ratings with scores 5 and 4 + assert Comment + |> Ash.Query.filter(exists(top_ratings, score == 5)) + |> Ash.read!() + |> length() == 1 + + assert Comment + |> Ash.Query.filter(exists(top_ratings, score == 4)) + |> Ash.read!() + |> length() == 1 + + assert Comment + |> Ash.Query.filter(exists(top_ratings, score == 3)) + |> Ash.read!() + |> length() == 0 + + assert Comment + |> Ash.Query.filter(exists(top_ratings, score == 1)) + |> Ash.read!() + |> length() == 0 + end + test "using `(is_nil(relationship) and other_relation_filter)` will trigger left join" do organization = Organization diff --git a/test/support/resources/comment.ex b/test/support/resources/comment.ex index 55d6f59a..3aa06c6f 100644 --- a/test/support/resources/comment.ex +++ b/test/support/resources/comment.ex @@ -113,6 +113,13 @@ defmodule AshPostgres.Test.Comment do relationship_context: %{data_layer: %{table: "comment_ratings"}} ) + has_many :top_ratings, AshPostgres.Test.Rating do + destination_attribute(:resource_id) + relationship_context(%{data_layer: %{table: "comment_ratings"}}) + sort(score: :desc) + limit(2) + end + has_many(:popular_ratings, AshPostgres.Test.Rating, public?: true, destination_attribute: :resource_id,