urschrei
urschrei

Reputation: 26859

How to calculate max using postgres dialect in sqlalchemy hybrid function

I've defined a hybrid property:

@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)

However the Postgres max function only operates on columns, unlike e.g. sqlite so the expression won't work. Is there something I could use in the Column elements or expressions, or some other way I can clamp my value to a lower bound of 0?

Upvotes: 0

Views: 109

Answers (1)

jjanes
jjanes

Reputation: 44137

You seem to be using the python function named 'max', not the PostgreSQL function.

If this somehow is postgresql, then max is for aggregates over rows. greatest is for multiple-arguments.

greatest(thing1,thing2)

Upvotes: 1

Related Questions