我在having条件下使用带有case的sqlalchemy func.sum,但出现以下错误。代码:
query = query.having(
func.sum(case([(e.c.escalation_type.in_(escalation_types), 1)], else_=0)) > 0
)上面的escalation_types is Python list获取此错误:
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.下面是上面打印的SQL:
HAVING sum(CASE WHEN (escalation_1.escalation_type IN (:escalation_type_1)) THEN :param_1 ELSE :param_2 END) > :sum_1这里我漏掉了什么?谢谢!
发布于 2020-08-28 17:35:53
看起来sqlalchemy的其中一个库asyncpg中有一个bug。我必须将1和0转换为整数才能使其工作。以下是工作代码:
query = query.having(
func.sum(
case(
[(e.c.escalation_type.in_(escalation_types), cast(1, Integer))],
else_=cast(0, Integer),
)
)
> 0
)https://stackoverflow.com/questions/63627858
复制相似问题