Postgresql COLLATE requires quotes

Issue #3785 resolved
Mike Bayer repo owner created an issue
import sqlalchemy as sa
e = sa.create_engine(
    'postgresql://scott:tiger@localhost/test', echo='debug')


m = sa.MetaData()
t = sa.Table('table', m, sa.Column('column', sa.TEXT()))
with e.connect() as conn:
    trans = conn.begin()
    t.create(bind=conn)

    # fails
    s = sa.select([t]).order_by(t.c.column.collate('C'))

    # works
    # s = sa.select([t]).order_by(t.c.column.collate('"C"'))

    trans.rollback()

the implementation here would need to build out a new Collate() construct that's intercepted by each dialect. Right now BinaryExpression(expr, _literal_as_text(collation)) is hardwired into elements.py.

PG dialect would also need to anticipate existing quotes and warn if they're present.

Comments (2)

  1. Mike Bayer reporter

    Treat collation names as identifiers

    The expression used for COLLATE as rendered by the column-level :func:.expression.collate and :meth:.ColumnOperators.collate is now quoted as an identifier when the name is case sensitive, e.g. has uppercase characters. Note that this does not impact type-level collation, which is already quoted.

    Change-Id: I83d5d9cd1e66a4f20b96303bb84c5f360d5d6a1a Fixes: #3785

    → <<cset 0883d8213bcf>>

  2. Log in to comment