4

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?

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
exabiche
  • 2,895
  • 2
  • 17
  • 10

1 Answers1

4

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.
Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
  • Ubuntu 10.10 hasn't 0.7 version in repo :( – exabiche May 30 '11 at 11:43
  • You can use `pip install sqlalchemy --upgrade`. Using `pip` makes you independent on your system's packaging, where are usually outdated/not-so-on-edge versions of Python libraries :-) – Honza Javorek Jan 06 '12 at 10:42