【发布时间】:2021-03-06 13:30:13
【问题描述】:
我是 python 和 sqlalchemy 的新手。 如果我手动构建 where 条件,我已经有一个删除方法可以工作。 现在,我需要从 yaml 格式的输入请求中读取列和值并创建 where 条件。
#enter data as yaml
items:
- item:
table: [MyTable,OtherTable]
filters:
field_id: 1234
#other_id: null
这是我尝试但无法继续的方法:
for i in use_case_cfg['items']:
item = i.get('item')
for t in item['table']:
if item['filters']:
filters = item['filters']
where_conditions = ''
count = 0
for column, value in filters.items():
aux = str(getattr(t, column) == bindparam(value))
if count == 0:
where_conditions += aux
else:
where_conditions += ', ' + aux
count += 1
to_delete = inv[t].__table__.delete().where(text(where_conditions))
#to_delete = t.__table__.delete().where(getattr(t, column) == value)
else:
to_delete = inv[t].__table__.delete()
CoreData.session.execute(to_delete)
对我来说,它看起来不错,但是当我运行时,我收到以下错误:
sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) 绑定参数“9876”需要一个值
[SQL:从MyTable删除“MyTable”.field_id = %(1234)s]
[参数: [{}]]
(此错误的背景:http://sqlalche.me/e/cd3x)
有人可以向我解释什么是错误的或正确的方法吗? 谢谢。
【问题讨论】:
标签: python sqlalchemy