I've been using hybrid properties recently and ran into a solution that 
would only work on PostgreSQL.  That's fine for my usage, but it's so ugly 
I was curious if there was a better, more portable way to do this.

Here is the scenario: x minutes before a given date things can happen in my 
program.  This is used for things like notifications and things where 
storing the actual date is more onerous than storing the delta itself. 
 Here is some code to give an idea (note that this is simplified example 
and self.minutes_before could actually come from a different table, so 
saving the calculated date would be impractical)

@hybrid_property
def notify_date(self)
    return self.date - timedelta(minutes=self.minutes_before)

@notify_date.expression
def notify_date(cls)
    return cls.date - cast(cast(cls.minutes_before, Unicode) + ' minutes', 
Interval)

So that last part is attempting to mimic this in PostgreSQL:

SELECT date - (minutes_before::varchar || ' minutes')::interval FROM 
my_table

So that's it.  Anyone aware of a better way to accomplish this?  Is there 
something in SQLAlchemy that would do something similar to timedelta? 
 Something like sqlalchemy.interval(minutes=cls.minutes_before) that can 
use attributes as well as literals and can translate between databases?


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/myAFyaY-m4EJ.
To post to this group, send email to sqlalchemy@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