Skip to content

Commit

Permalink
Merge pull request #174 from antonsatin/fix-custom-distinct
Browse files Browse the repository at this point in the history
Fix for custom `distinct` handling in laterals
  • Loading branch information
benwilson512 authored Dec 19, 2024
2 parents dff6fd3 + 9d9d3eb commit ee9e011
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,8 @@ if Code.ensure_loaded?(Ecto) do
build_preload_lateral_query(rest, join_query, :join_last)
end

defp maybe_distinct(%Ecto.Query{distinct: dist} = query, _) when dist, do: query
defp maybe_distinct(%Ecto.Query{distinct: distinct} = query, _) when not is_nil(distinct),
do: query

defp maybe_distinct(query, [%Ecto.Association.Has{}, %Ecto.Association.BelongsTo{} | _]),
do: distinct(query, true)
Expand Down
6 changes: 3 additions & 3 deletions test/dataloader/ecto/limit_query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ defmodule Dataloader.LimitQueryTest do
|> preload(likes: :user)
end

defp query(schema, %{limit: limit, order_by: order_by}, test_pid) do
defp query(schema, %{limit: limit, distinct: true, order_by: order_by}, test_pid) do
send(test_pid, :querying)

schema
|> order_by(^order_by)
|> limit(^limit)
|> distinct(true)
end

defp query(schema, %{limit: limit, distinct: true, order_by: order_by}, test_pid) do
defp query(schema, %{limit: limit, order_by: order_by}, test_pid) do
send(test_pid, :querying)

schema
|> order_by(^order_by)
|> limit(^limit)
|> distinct(true)
end

test "Query limit does not apply globally", %{loader: loader} do
Expand Down

0 comments on commit ee9e011

Please sign in to comment.