简体   繁体   English

在 sqlalchemy 中使用 sum 和 case 时出错

[英]get error when using sum and case in sqlalchemy

I'm using sqlalchemy func.sum with case in a having condition but get below error.我使用sqlalchemy func.sumcasehaving条件,但得到下面的错误。 code:代码:

query = query.having(
            func.sum(case([(e.c.escalation_type.in_(escalation_types), 1)], else_=0)) > 0
        )

escalation_types above is Python list get this error:上面的escalation_types是 Python 列表得到这个错误:

asyncpg.exceptions.UndefinedFunctionError: function sum(text) does not exist
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Here is the SQL printed by above:这是上面打印的SQL

HAVING sum(CASE WHEN (escalation_1.escalation_type IN (:escalation_type_1)) THEN :param_1 ELSE :param_2 END) > :sum_1

what am I missing here?我在这里错过了什么? Thanks!谢谢!

Looks like there is a bug in one of the libraries of sqlalchemy , asyncpg .貌似在图书馆的一个错误sqlalchemyasyncpg I have to cast 1 and 0 to integer to make it work.我必须将 1 和 0 转换为整数才能使其工作。 here is working code:这是工作代码:

query = query.having(
            func.sum(
                case(
                    [(e.c.escalation_type.in_(escalation_types), cast(1, Integer))],
                    else_=cast(0, Integer),
                )
            )
            > 0
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM