【发布时间】:2020-01-07 06:58:57
【问题描述】:
以下错误的最可能原因是什么?我该如何解决?我不明白为什么 SQLAlchemy 会生成一个缺少表达式的查询!该代码适用于 SQLite,但在 Oracle 上失败。下面的 SQL 查询有什么问题?
此 Python 代码适用于 SQLite,但在 Oracle 11g 上失败:
q = (session.query(locomotion_class).join(LocomotionLink, LocomotionLink.parent_id == self.id).filter(LocomotionLink.child_id == locomotion_class.id, LocomotionLink.child_id != self.id))
# locomotion_class is any of the subclasses of Locomotion (in this case Virion)
return session.query(q.exists()).scalar()
以上结果如下查询:
SELECT EXISTS (
SELECT 1
FROM locomotion
JOIN virion ON locomotion.id = virion.id
JOIN locomotion_link ON locomotion_link.parent_id = 16
WHERE locomotion_link.child_id = virion.id AND locomotion_link.child_id != 16
) AS anon_1 FROM DUAL
它在 SQLite 上工作,但在 Oracle 11g 上失败
"Internal Server Error: (cx.Oracle.DatabaseError) ORA-00936: missing expression"
当我尝试在 sqlplus 中运行生成的表达式时,它在“parent_id = 16”中显示星号低于 6(即直接在 WHERE 之前),但我看不到有任何遗漏。
【问题讨论】:
-
我认为你应该阅读docs.sqlalchemy.org/en/13/orm/…中关于“一些数据库”的注释
-
谢谢,您的评论很有帮助!
标签: python sql oracle sqlalchemy