Skip to content

Commit

Permalink
Safe distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
cspeper committed Apr 19, 2022
1 parent a00641d commit fdbaeb4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,15 @@ if Code.ensure_loaded?(Ecto) do
end

defp maybe_distinct(query, [%Ecto.Association.Has{}, %Ecto.Association.BelongsTo{} | _]) do
distinct(query, true)
%Ecto.Query{distinct: dist} = query
if dist, do: query, else: distinct(query, true)
end

defp maybe_distinct(query, [%Ecto.Association.ManyToMany{} | _]) do
%Ecto.Query{distinct: dist} = query
if dist, do: query, else: distinct(query, true)
end

defp maybe_distinct(query, [%Ecto.Association.ManyToMany{} | _]), do: distinct(query, true)
defp maybe_distinct(query, [_assoc | rest]), do: maybe_distinct(query, rest)
defp maybe_distinct(query, []), do: query

Expand Down

0 comments on commit fdbaeb4

Please sign in to comment.