出于某种原因,SQLAlchemy正在引用我的查询的LIKE过滤器值!
代码是这样的:
query = app.db.session.query(model.Attribute)\
.filter(model.Attribute.name == 'photo0')\
.filter(model.Attribute.value.like('%' + file + '%'))\
.all()
非常简单直接。它应该产生类似value LIKE '%78744154439.htm%'
的东西,但SQLAlchemy正在添加额外的双引号。
这里是来自此查询的DB回声:
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%"')
我试图逃避%认为这是因为字符串转义,但仍然没有!
我使用SQLAlchemy 0.9.6和MySQL-python 1.2.5作为我的DBAPI运行Python 2.7.6。
答案 0 :(得分:0)
您可以使用以下语法解决此问题:
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