如何在 sqlalchemy 混合函数中使用 postgres 方言计算最大值

How to calculate max using postgres dialect in sqlalchemy hybrid function

我定义了一个混合体 属性:

@hybrid_property
def result(self):
    return max(0, self.impact - self.protection)

@result.expression
def result(cls):
    return func.max(0, cls.impact - cls.protection)

但是 Postgres max 函数仅对列进行操作,这与例如sqlite 所以表达式不起作用。有什么我可以在 Column elements or expressions 中使用的东西,或者我可以通过其他方式将我的值限制在 0 的下限吗?

您似乎在使用名为 'max' 的 python 函数,而不是 PostgreSQL 函数。

如果这是 postgresql,那么 max 是针对行的聚合。 greatest 用于多参数。

greatest(thing1,thing2)