title: How do I implement a null coalescing operator in SQLAlchemy?
body: <p>Or how do I make this thing work?</p>
  
  <p>I have an Interval object:</p>
  
  <pre><code>class Interval(Base):
      __tablename__ = 'intervals'
      id = Column(Integer, primary_key=True)
      start = Column(DateTime)
      end = Column(DateTime, nullable=True)
      task_id = Column(Integer, ForeignKey('tasks.id'))
  
  @hybrid_property #used to just be @property
  def hours_spent(self):
      end = self.end or datetime.datetime.now()
      return (end-start).total_seconds()/60/60
  </code></pre>
  
  <p>And a Task:</p>
  
  <pre><code>class Task(Base):
      __tablename__ = 'tasks'
      id = Column(Integer, primary_key=True)
      title = Column(String)
      intervals = relationship("Interval", backref="task")
  
  @hybrid_property  # Also used to be just @property
  def hours_spent(self):
      return sum(i.hours_spent for i in self.intervals)
  </code></pre>
  
  <p>Add all the typical setup code, of course.</p>
  
  <p>Now when I try to do <code>session.query(Task).filter(Task.hours_spent &gt; 3).all()</code></p>
  
  <p>I get <code>NotImplementedError: &lt;built-in function getitem&gt;</code> from the <code>sum(i.hours_spent...</code> line.</p>
  
  <p>So I was looking at <a href="http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/hybrid.html#building-custom-comparators" rel="noreferrer">this part</a> of the documentation and theorized that there might be some way that I can write something that will do what I want. <a href="http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/hybrid.html#correlated-subquery-relationship-hybrid" rel="noreferrer">This part</a> also looks like it may be of use, and I'll be looking at it while waiting for an answer here ;)</p>