如果我有sqlalchemy:
select([foo.c.id]).select.where(foo.c.id == bar.c.val).select_from(foo)
我得到的SQL看起来像:
SELECT foo.id
FROM foo, bar
WHERE foo.id = bar.val
我想要的是:
SELECT foo.id
FROM foo
WHERE foo.id = bar.val
我意识到这本身没有意义,但我正在编译表达式并将其嵌套在表bar
上的delete语句的exists子句中,因此bar.val
有意义在上下文中 - 完整的陈述将类似于:
DELETE FROM bar WHERE EXISTS (
SELECT foo.id
FROM foo
WHERE foo.id = bar.val
)
如何在SQL alchemy中关闭自动FROM
子句创建?
答案 0 :(得分:0)
对于您的情况,您无需将其关闭。只需创建一个完整的delete
语句,sqlalchemy
就会明白:
from sqlalchemy.sql.expression import exists
q2 = bar.delete().where(exists(q1))
print(q2)
产生
DELETE FROM bar WHERE EXISTS (SELECT foo.id
FROM foo
WHERE foo.id = bar.id)