【问题标题】:SQLAlchemy: ObjectNotExecuteableError on joinsSQLAlchemy:连接时出现 ObjectNotExecuteableError
【发布时间】:2018-10-10 17:38:04
【问题描述】:

我目前正在为 SQLAlchemy 中的一个语句而苦苦挣扎。基本上 SQL 脚本本身正在运行,看起来像'

SELECT DISTINCT t1.col1, t1.col2, t2.col1, t2.col2, t2.col3, t3.col1
FROM t1
JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 on t1.id = t3.id and t3.col2 = 12345

现在,将此语句拆分为 SQLAlchemy,我最终得到了这样的结果:

stmt = select([t1.c.col1, t1.c.col2, t2.c.col1, t2.c.col2, t2.c.col3, t3.c.col1]).distinct().\
     select_from(table(t1)).\
     join(table(t2), t1.c.id == t2.c.id).\
     outerjoin(table(t3), and_((t1.c.id == t3.c.id), (t3.c.col2 == 12345))

我猜是因为与众不同。我也在outerjoin之后将它放在整行的末尾,但是我得到一个属性错误,说join没有不同的属性。

对我有什么提示吗?

提前非常感谢,问候,托马斯

【问题讨论】:

  • 我也省略了 distinct 并试图让它在没有的情况下运行,错误仍然存​​在,我得到一个 ObjectNotExecutableError。我试图打印它,但print(str(stmt)) 返回另一个错误:`'str' object has no attribute '_compiler_dispatch'`

标签: python sql python-3.x select sqlalchemy


【解决方案1】:

我自己都不敢相信,但我找到了问题的答案。这或多或少是如何将连接连接在一起的方式。

我上面查询的解决方案是:

stmt = select([t1.c.col1, t1.c.col2, t2.c.col1, t2.c.col2, t2.c.col3, t3.c.col1]).\
     select_from(
         table(t1).\
         join(table(t2), t1.c.id == t2.c.id).\
         outerjoin(table(t3), and_((t1.c.id == t3.c.id), (t3.c.col2 == 12345)))
     ).distinct()

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2019-03-19
    • 2011-03-22
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多