[英]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.sum
用case
在having
条件,但得到下面的错误。 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
.貌似在图书馆的一个错误
sqlalchemy
, asyncpg
。 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.