编辑我找到了答案,它与模型的设置方式无关。我试图将request.form
from 烧瓶传递给实际创建地址的函数。显然将其转换为kwargs
会导致问题。手动指定每个参数使一切正常。
我是 SQLAlchemy 的新手,到目前为止一切进展顺利,但我遇到了一个问题,我完全被难住了,所以我希望这里有人知道我做错了什么!
我有一个地址模型,我正在尝试从中进行选择。该模型如下所示:
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
comment = Column(String(255))
address1 = Column(String(100))
address2 = Column(String(100))
city = Column(String(45))
state = Column(String(2))
zip = Column(String(5))
zip4 = Column(String(4))
lat = Column(Float)
lon = Column(Float)
我正在尝试匹配通过我的应用程序输入的地址的元素,以查看是否可以在不进行地理编码的情况下找到纬度和经度。我filter_by
在其他有效的查询中使用过,所以我在这里尝试同样的事情:(address
字典来自我的应用程序的发布参数)
matched_address = session.query(Address).filter_by(
address1=address['address1'],
city=address['city'],
state=address['state'],
zip=address['zip']
).one()
不幸的是,这因 SQL 错误而失败:
ProgrammingError: (ProgrammingError) (1064, 'You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right
syntax to use near \') AND address.state = ("\'XX\'",) AND address.zip =
("\'XXXXX\'",) AND address.city \' at line 3')
这看起来非常非常难看!额外的()
从哪里来?我打开echo=True
了我的电话create_engine()
,它打印了一个看起来正确的查询(我在 mysql 客户端中尝试过,没有问题):
SELECT address.id AS address_id, address.comment AS address_comment,
address.address1 AS address_address1, address.address2 AS address_address2,
address.city AS address_city, address.state AS address_state,
address.zip AS address_zip, address.zip4 AS address_zip4,
address.lat AS address_lat, address.lon AS address_lon
FROM address
WHERE address.address1 = %s AND address.state = %s AND address.zip = %s AND
address.city = %s
LIMIT %s
但是它说它输出的参数似乎有点奇怪,虽然我不确定这是否只是 python 对象,或者它是否真的以这种方式进入 MySQL:(注意 X 字符只是我真实地址的替换)
([u'1111 XXXXXX St'], [u'XX'], [u'XXXXX'], [u'XXXXX'], 1)
只是为了好玩,我尝试使用filter()
而不是,filter_by()
但我得到了同样的错误:
matched_address = database.session.query(Address).filter(
and_(Address.address1==address['address1'], Address.city==address['city'],
Address.state==address['state'],Address.zip==address['zip'])).one()
对不起,如果这是一个基本问题。提前感谢您的帮助!