1. Michael Bayer
  2. sqlalchemy
  3. Issues

Issues

Issue #3098 closed

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

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. Log in to comment