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

DISTINCT ON requires matching ORDER clause #24

Open
rangerscience opened this issue Jun 30, 2023 · 0 comments
Open

DISTINCT ON requires matching ORDER clause #24

rangerscience opened this issue Jun 30, 2023 · 0 comments

Comments

@rangerscience
Copy link

rangerscience commented Jun 30, 2023

Per Postgres docs - and experimentation! ;) - specify that the DISTINCT ON clause must have a matching ORDER BY, such that the columns used in the distinct clause are the left-most in the ORDER clause. So you end up needing to do:

Model.distinct_on(:col).order(:col)

It would be nice (I think?) if this was handled automagically. (Or is this supposed to be automagically handled by AREL?)

Bonus - This plays weird with other things that apply ordering (ActiveAdmin). My guess (and hope) is that there's a way to say "no, this ordering always is left-most", so that when something like AA adds their ordering, it doesn't break the DISTINCT ON.

@rangerscience rangerscience changed the title DISTINCT ON require ORDER DISTINCT ON requires matching ORDER clause Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant