The descriptor works by returning an SQL Expression when called on a
class argument:

  Interval.length -> SQL Expression
  Interval.contains(arg) -> SQLExpression

and operates normally on instances, ie, just runs the function.

On Jan 15, 11:41 pm, bojanb <boj...@gmail.com> wrote:
> Thanks Mike. I must admit I don't understand why that code works, but
> it does. I guess that's the "Alchemy" in "SQLAlchemy" :-)
>
> However, I have issues with the difference in NULL value semantics
> between Python and SQL. Ie. if a calculated column is defined via a
> column_property as price*amount, then the result will be NULL if any
> of the values is NULL. However, in Python, None*something throws a
> TypeError, so the hybrid_property getter function needs to be filled
> with lots of IFs.

When called as class properties, the descriptors always generate
SQL expressions as above.

When called as instance properties, it just calls your function and
you can
do what you like with None values, e.g.:

@hybrid_property
def length(self):
    return self.thing is not None and self.thing or None

> Also, this solution can't be used for date calculations, as timedelta
> objects are needed. So I guess I will stick with a mix of Python

The example works in postgresql with timestamps and intervals/
timedeltas. ;-)

a.
-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


Reply via email to