AttributeError:'_ query_cls'

时间:2017-03-15 15:29:54

标签: python mysql events sqlalchemy subquery

我试图通过事件before_compile将一些sql添加到被调用的查询中。我检查被查询的类的类型,看看它是否需要额外检查。

但是我在执行时遇到错误:
AttributeError:'Label'对象和'Comparator'对象都没有属性'_query_cls'

SQL

SELECT og.* FROM $$$ og JOIN (
SELECT uu.id as u_id, i.id as s_id FROM upload uu JOIN (
        SELECT MAX(u.upload) as maxu, s.id FROM $$$ s
        JOIN upload u ON s.upload_id = u.id
        GROUP BY s.id
    ) i ON uu.upload = i.maxu
) sss ON og.id = sss.s_id AND og.upload_id = sss.u_id

-- Where $$$ is a Base class

的Python

@event.listens_for(Query, "before_compile", retval=True)
def do_filtered_load(query):
    class_name = query.column_descriptions[0]["entity"]

    if class_name in pre_classes:
        t = Session.query(func.max(Upload.upload).label("maxu"), class_name.id.label("s_id")).group_by(
            class_name.id).subquery()
        s = Session.query(Upload.id.label("u_id"), t.c.s_id.label("s_id")).join(t.c.maxu == Upload.upload).subquery()

        query = query.join(s, s.c.s_id == class_name.id and class_name.upload_id == s.c.s_u_id)
    return query

我做错了什么?

0 个答案:

没有答案