Sqlalchemy with postgres. Try to get 'DISTINCT ON' instead of 'DISTINCT'

僤鯓⒐⒋嵵緔 提交于 2019-12-23 15:52:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!