postgres_where does not quote literal strings in predicate expression

Issue #1640 resolved
Former user created an issue

In code like this:

Index('ix_foo',
      sometable.c.somecol,
      postgres_where=sometable.c.type=='somevalue')

I expect the index creation DDL to be:

CREATE UNIQUE INDEX ix_foo ON sometable (somecol) WHERE sometable.type = 'somevalue'

but it is:

CREATE UNIQUE INDEX ix_foo ON sometable (somecol) WHERE sometable.type = somevalue

So the string literal is not quoted, as it is when I specify WHERE conditions in queries. This violates the principle of least astonishment.

As a workaround, nested quoting ("'base'") works.

Comments (7)

  1. Mike Bayer repo owner
    • changed milestone to 0.6.0

    I'm fairly certain this was fixed in f9cb6f5834fb1acf4460fd9bb6b72f8c76f8c36c. A test that is specific towards strings should be added to test_postgresql.py. This is targeted at 0.6 only since backwards-compatible "guessing" for whether or not quotes were applied in 0.5 is not really worth it.

  2. Former user Account Deleted

    However, the current approach will fail if the string contains '-characters, such as

            idx = Index('test_idx1', tbl.c.data, postgresql_where=and_(tbl.c.data > 'a', tbl.c.data < "b's"))
    

    It results in CREATE INDEX test_idx1 ON testtbl (data) WHERE data > 'a' AND data < "b's", which is wrong because the latter is quoted as an identifier.

  3. Mike Bayer repo owner

    this is why we never wanted to get into this. we essentially have to duplicate the quoting capability of the DBAPI and/or client library. oh well

  4. Log in to comment