Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for custom distinct handling in laterals #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,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