我有以下构造似乎产生了所需的SQL:
>>> print session.query(exists('1').where(MyTable.name=='x'))
SELECT EXISTS (SELECT 1
FROM my_table
WHERE my_table.name = :name_1) AS anon_1
但是,当我尝试使用.scalar()
或.all()
执行它时,它会返回错误:
*** UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session
如何为这个简单的查询绑定它?我不想做bool(MyTable.query.filter(MyTable.name=='x').first())
,因为浪费地从表中拉回整行。
更新
我也试过了:
>>> session.connection(mapper=MyTable).execute(
exists('1').where(MyTable.name=='x'))
StatementError: Not an executable clause 'EXISTS \
(SELECT 1 \nFROM my_table \nWHERE my_table.name = %(name_1)s)' []
答案 0 :(得分:1)
我认为:
>>> session.connection(mapper=MyTable).execute(
select([exists('1').where(MyTable.name=='x')]))