问题
I need to generate query like this:
SELECT **DISTINCT ON** (article.code) article.code, article.title
First I try to make it via ORM distinct method and send it a list with fields. But it wont work. Second, I try to make it via sqlalchemy.sql.select - and it also generate sql query like this:
SELECT DISTINCT article.code, article.title
I Need SELECT **DISTINCT ON** (article.code)
...
I look at source code and found in sqlalchemy.dialects.postgresql.base.PGCompiler.get_select_precolumns
code for generating constructions like: 'DISTINCT ON'
But this method do not called. Instead of this called another method - sqlalchemy.sql.compiler.get_select_precolumns
- it hasn't code for generating DISTINCT ON
only for DISTINCT
Maybe I should configure my session to called properly method?
回答1:
This bug report suggests that DISTINCT ON
works correctly in SQLAlchemy 0.7+. I think an upgrade is in order, unless you've uncovered a bug in 0.7.
Workarounds . . .
- Volunteer to help get the 0.7 package ready for Ubuntu.
- Download and install from source.
- Rewrite queries to avoid
DISTINCT ON
. I'm not sure whether that's possible in the most general case.
来源:https://stackoverflow.com/questions/6175657/sqlalchemy-with-postgres-try-to-get-distinct-on-instead-of-distinct