Sqlalchemy with postgres. Try "DISTINCT ON" instead of "DISTINCT"

I need to generate a query as follows:

SELECT **DISTINCT ON** (article.code) article.code, article.title 

First I try to do this using the ORM method and send him a list with fields. But that will not work. Secondly, I am trying to do this through sqlalchemy.sql.select - and it will also generate a sql query as follows:

 SELECT DISTINCT article.code, article.title 

I need SELECT **DISTINCT ON** (article.code) ...

I look at the source code and found sqlalchemy.dialects.postgresql.base.PGCompiler.get_select_precolumns in the code to create constructions such as: "DISTINCT ON" But this method is not called. Instead, another method is called - sqlalchemy.sql.compiler.get_select_precolumns - it does not have code to generate DISTINCT ON only for DISTINCT Maybe I need to configure my session to the correct method?

+2
source share
1 answer

This error report assumes that DISTINCT ON working correctly in SQLAlchemy 0.7+. I think the update is fine if you did not find an error in 0.7.

Temporary solutions.,.

  • Volunteer to help get package 0.7 ready for Ubuntu.
  • Download and install from source.
  • Rewrite queries to avoid DISTINCT ON . I am not sure if this is possible in the most general case.
+1
source

All Articles