SQLAlchemy is not smart enough to build SQL expression tree from these operands, you have to use explicit propname.expression
decorator to provide it. But then comes another problem: there is no portable way to convert interval to hours in-database. You'd use TIMEDIFF
in MySQL, EXTRACT(EPOCH FROM ... ) / 3600
in PostgreSQL etc. I suggest changing properties to return timedelta
instead, and comparing apples to apples.
from sqlalchemy import select, func
class Interval(Base):
...
@hybrid_property
def time_spent(self):
return (self.end or datetime.now()) - self.start
@time_spent.expression
def time_spent(cls):
return func.coalesce(cls.end, func.current_timestamp()) - cls.start
class Task(Base):
...
@hybrid_property
def time_spent(self):
return sum((i.time_spent for i in self.intervals), timedelta(0))
@time_spent.expression
def hours_spent(cls):
return (select([func.sum(Interval.time_spent)])
.where(cls.id==Interval.task_id)
.label('time_spent'))
The final query is:
session.query(Task).filter(Task.time_spent > timedelta(hours=3)).all()
which translates to (on PostgreSQL backend):
SELECT task.id AS task_id, task.title AS task_title
FROM task
WHERE (SELECT sum(coalesce(interval."end", CURRENT_TIMESTAMP) - interval.start) AS sum_1
FROM interval
WHERE task.id = interval.task_id) > %(param_1)s