SQLAlchemy double-quoting LIKE filter
Question: For some reason, SQLAlchemy is double-quoting the LIKE filter value of my
queries!

The code is this:

    
    
    query = app.db.session.query(model.Attribute)\   
      .filter(model.Attribute.name == 'photo0')\
      .filter(model.Attribute.value.like('%' + file + '%'))\
      .all()
    

Pretty simple and straight forward. It should produce something like `value
LIKE '%78744154439.htm%'` but SQLAlchemy is adding additional double-quotes.

Here's the DB echo from this query:

    
    
    2014-07-17 14:32:24,472 INFO sqlalchemy.engine.base.Engine SELECT attributes.listing_id AS attributes_listing_id, attributes.id AS attributes_id, attributes.name AS attributes_name, attributes.value AS attributes_value 
    FROM attributes 
    WHERE attributes.name = %s AND attributes.value LIKE %s
    2014-07-17 14:32:24,472 INFO sqlalchemy.engine.base.Engine ('photo0', '"%78744154439.htm%"')
    

I've tried to escape the % thinking it was due to string escaping, but still
nothing!

I'm running Python 2.7.6 with SQLAlchemy 0.9.6 and MySQL-python 1.2.5 as my
DBAPI.


Answer: You can get around this by using the following syntax:

    
    
    from sqlalchemy.sql import text
    
    query = app.db.session.query(model.Attribute)\      
           .filter(model.Attribute.name == 'photo0')\   
           .filter(model.Attribute.value.like(text("'%" + file + "%'"))\   
           .all()
    

HTH

