Percent sign formatting of ddl strings breaks for modulo operations on columns that start with f, d, etc.

Issue #3098 closed
mike_solomon created an issue

The following code does not compile because there is a % f in it (from the modulo operation on the table foo) and the visit_ddl function in compiler.py uses python's percent sign formatting syntax.

from sqlalchemy import Table, MetaData, Column, Integer, DDL
from sqlalchemy import create_engine, event

ECHO = False
engine = create_engine('sqlite:///memory', echo=ECHO)

ddl = DDL(
'''CREATE TRIGGER foo AFTER INSERT ON bar
      BEGIN
          INSERT INTO bar SELECT * FROM foo WHERE 2 % foo.id = 0;
      END;
    '''  
)

md = MetaData()
Foo = Table('foo', md, Column('id', Integer))
Bar = Table('bar', md, Column('id', Integer))

event.listen(Foo, 'after_create', ddl.execute_if(dialect='sqlite'))

Foo.metadata.create_all(engine)

Comments (6)

  1. mike_solomon reporter

    For now, my workaround is that I'm creating a function mod that I'm sending to the sqlite backend, so as soon as this is fixed, I'll replace the mod function with the % sign operator.

  2. mike_solomon reporter

    It'd be helpful if this error was caught before being raised and printed a message to the command line to the effect of "your DDL statement may have percent signs in it. If this is the case and they are not escaped, please escape them as %%" before it raised the error definitively.

  3. Mike Bayer repo owner

    percent signs are allowed because there are supported replacement tokens in the DDL string.

  4. Log in to comment