execute fails on postgres if % in statement and no parameters

Issue #4292 wontfix
Michael Del Monte
created an issue

This fails

import sqlalchemy
e = sqlalchemy.create_engine("postgresql://postgres@localhost:5432/postgres")
e.scalar("select 1 where 'a' like 'a%'")


TypeError: 'dict' object does not support indexing

because the underlying execution context wants positional arguments so it supplies an empty dict as the "parameters" argument, but psycopg2.cursor.execute wants a tuple.

Comments (3)

  1. Michael Bayer repo owner

    unfortunately percent signs at the raw execute level must be doubled and it would break backwards compatibility to change this, SQLAlchemy always passes a parameter set for consistency.

    Use text() instead:

    e.execute(text("select 1 where 'a' like 'a%'"))
  2. Michael Bayer repo owner

    I'm not happy about it but it's not worth the disruption to change. since execute() is kind of opaque it would make other things more confusing, especially how statements are compiled.

  3. Log in to comment