Parameters in pgsql DISTINCT ON () not getting bound

Issue #4325 resolved
Robert Bertorelli
created an issue


I've run into the following issue in sqlalchemy 1.2.11. I looked through the already-extant bugs and didn't find this.

In the postgresql dialect, parameters within a DISTINCT ON clause will not be bound.

Here's a simple example using python 3.6:

>>> import sqlalchemy as sa
>>> from sqlalchemy.dialects import postgresql as pg
>>> col = sa.column('name')
>>> col2 = col == 'foo'
>>> sel =[col2]).distinct(col2)
>>> print(sel.compile(dialect=pg.dialect(), compile_kwargs={"literal_binds": True}))
SELECT DISTINCT ON (name = %(name_1)s) name = 'foo' AS anon_1

So, as you can see, the parameter in the SELECT statement (name = 'foo') did get bound properly, but the same column in the DISTINCT ON did not.

I believe that the issue can be found here:

Seems like **kw needs to get passed into self.process().


Comments (4)

  1. Michael Bayer repo owner

    Propagate **kw in postgresql distinct on compilation

    Fixed bug in PostgreSQL dialect where compiler keyword arguments such as literal_binds=True were not being propagated to a DISTINCT ON expression.

    Fixes: #4325 Change-Id: I9949387dceb7fabe889799f42e92423572368b29

    → <<cset 469931514a15>>

  2. Log in to comment