This project is archived and is in readonly mode.
Has many through association with :order causes a SQL error with PostgreSQL
-
Frederick Cheung
Won't this cause the column use for ordering to appear (piggy backed) in the person objects thus loaded ? Probably relatively harmless usually but could cause grief if it ends up shadowing a column (at least for users of other databases).
-
Winky
I'm not familiar with rails internals and attribute processing, but definitely a risk. Might consider skipping attribute processing for fields not from the primary table in this case, but then the SQL building will have to be altered to ensure that all ORDER BY fields on the association table are fully qualified with the table name.
I also found that removing the :order option to the association fixes the problem, but I'm not sure that solution is any better if you're dealing with an order-sensitive association.
I defer to you guys to determine the best and most elegant solution.
-
Bjørn Arild Mæland
Is anyone working on this patch? The error is very annoying as it also occurs if the associated model defines a default_scope with ordering.
-
Gravis
+1 I fell into this problem today.
I'm using a :select => "DISTINCT column_thing" and the generated SQL is failing because of the default order by not part of the selected rows (which I don't want of course).Thanks
-
Sean
+1 experiencing the same problem. In my case, removing DISTINCT from the SQL statement (or :uniq from the AR command) solves the problem.
-
Jon Leighton
- Assigned user set to Aaron Patterson
- Importance changed from to
It's not clear what the expected result should be, which is why postgres throws an error.
Due to the use of DISINCT, for any given
Personin the output, there may be many differentrenewal_datevalues. Which is the correct one to use for the sort? The answer is ambiguous, hence why postgres gives an error.I'm not in favour of Active Record trying to magically resolve this ambiguity by making an assumption, so I'd say we should close the ticket.
-
Aaron Patterson
- State changed from incomplete to invalid
I agree with Jon. Closing this ticket as invalid.
-
Lance Carlson
I disagree with Jon. Since the assumption is that memberships should be unique (:uniq => true), the Person object should only have one membership and thus should only have one renewal_date to compare against. The intent is pretty explicit in this case and thus I feel AR can make the solid assumption that there won't be ambiguity amongst multiple membership objects.
I think this ticket should be re-opened and solved because I'm pretty sure this behavior works (magically) in MySQL with this exact syntax, so Postgres should behave the same.
The only work around I've found for this has been to eliminate the :uniq property and specify:
:select => 'DISTINCT people.*, memberships.renewal_date', :order => "memberships.renewal_date ASC"
which is not a full solution because with :uniq disabled, multiple memberships objects can get created.
Since my app is heavily JSON based, I've resorted to crafting the JSON instead of trying to hack AR. The end result is more queries, but I had to solve it somehow. :)
+1000 to testing this patch and pulling it in if it works!
-
Lance Carlson
Created a github issue:
