与postgres的Sqlalchemy。尝试让“ DISTINCT ON”代替“ DISTINCT” [英] Sqlalchemy with postgres. Try to get 'DISTINCT ON' instead of 'DISTINCT'
问题描述
我需要生成这样的查询:
I need to generate query like this:
SELECT **DISTINCT ON** (article.code) article.code, article.title
首先,我尝试通过ORM独特的方法进行创建,并向其发送包含字段的列表。但它不会工作。其次,我尝试通过sqlalchemy.sql.select进行创建-并且还会生成如下这样的sql查询:
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
我需要 SELECT ** DISTINCT ON ** (文章代码)
...
我查看源代码并在 sqlalchemy.dialects.postgresql中找到。 base.PGCompiler.get_select_precolumns
代码,用于生成以下结构:'DISTINCT ON'
但是未调用此方法。代替此方法的是另一种方法- sqlalchemy.sql.compiler.get_select_precolumns
-它没有用于生成 DISTINCT ON
的代码仅用于 DISTINCT
也许我应该将会话配置为正确调用的方法?
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?
推荐答案
此错误报告表明 DISTINCT ON
在SQLAlchemy 0.7+中可以正常工作。我认为升级是有序的,除非您发现0.7中的错误。
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.
解决方法。 。
- 志愿者帮助为Ubuntu准备好0.7软件包
。 - < a href = http://www.sqlalchemy.org/download.html rel = nofollow>下载并从
源安装。 - 重写避免
DISTINCT
的查询。我不确定在最一般的情况下是否有可能
ON
。
- 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.
这篇关于与postgres的Sqlalchemy。尝试让“ DISTINCT ON”代替“ DISTINCT”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!