从aggregate_order_by()使用select()时发生SQLAlchemy Postgres UnsupportedCompilationError

时间:2019-07-01 07:46:04

标签: python postgresql sqlalchemy

aggregate_order_by()中使用SQLAlchemy似乎无法呈现select([])

我有一个这样的PostgreSQL表达式:

ARRAY(
    SELECT DISTINCT UNNEST(
        array_agg(
            CASE WHEN parent_recalls.id IS NOT NULL THEN parent_recalls.oem_id ELSE recalls_recall.oem_id END) 
            ORDER BY recalls_recall.rank DESC
        )
    )
) AS oem_id_arr

当我使用SQLAlchemy时:

from sqlalchemy import func, select, case
from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import aggregate_order_by, array_agg

# RecallsRecall and ParentRecall are sqlalchemy.schema.Table objects
stmt = func.array(
    select([
        func.unnest(
            array_agg(
                aggregate_order_by(
                    case([(
                        ParentRecall.c.id != None,
                        ParentRecall.c.oem_id
                    )], else_=RecallsRecall.oem_id),
                    RecallsRecall.rank.desc()
                )
            )
        )]
    ).distinct()
).label('oem_id_arr')

print stmt.compile(dialect=postgresql.dialect())

我知道了

UnsupportedCompilationError: Compiler <sqlalchemy.sql.compiler.StrSQLCompiler object at 0x7fb3d8bf3d50> can't render element of type <class 'sqlalchemy.dialects.postgresql.ext.aggregate_order_by'>

我做错了什么?

谢谢!

0 个答案:

没有答案