-
Notifications
You must be signed in to change notification settings - Fork 330
Enhance distinct error #696
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
base: master
Are you sure you want to change the base?
Enhance distinct error #696
Conversation
) | ||
when is_list(exprs) do | ||
error!(query, "To apply DISTINCT to multiple columns at once, use distinct: true") | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! But won't this exact case already be caught by the expression below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is that the query param has to have select *
, this is being interpreted by the query
match pattern %Ecto.Query{select: %SelectExpr{expr: {:&, [], [0]}}}
I think this AST {:&, [], [0]}
means user is not trying to select specific fields
Thanks for your review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it will already raise with the error message below. Perhaps we should change the error below instead to say use distinct: true
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ecto actually selects the fields from the schema individually. So even if you don’t specify the fields yourself Ecto will. It won’t do select *.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry i got it now you mean this case
defp distinct(%ByExpr{expr: exprs}, _sources, query) when is_list(exprs) do
error!(query, "DISTINCT with multiple columns is not supported by MySQL")
end
Why i didnt think about it because in sql you cant do something like this anyway:
SELECT col1,col2 DISTINCT col1, col2
but you can do this
SELECT DISTINCT col1, col2
First case equivalent to distinct(%ByExpr{expr: exprs}, _sources, query) when is_list(exprs)
Second equivalent to the new one with the SelectExpr
pattern match which should work in Mysql
What do you think? should i delete new pattern match and simply just change the below case msg error
or make ecto works with the new case with new pattern match like its working in Mysql?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greg-rychlewski Thanks for the clarification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, to clarify, I am saying that, instead of adding a whole new clause, you could update the existing one to add more information. There is no reason to have two very similar code paths. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 🫡
1609bec
to
bdfa049
Compare
Related issue
elixir-ecto/ecto#4671
Changes
Changing the error message when distinct has more than a field