title: How to make SQLAlchemy hybrid_property select expressions work for both select and filter
body: <p>I've been attempting to write some expressions with hybrid properties, but I've found them to be extremely limited and I'm wondering if I can get around those limits.</p>
  
  <p>Basically, I've found they either work with <code>session.query(Model.hybrid_property)</code> or <code>session.query(Model).filter(Model.hybrid_property==x)</code>, but not both.</p>
  
  <p>Here's an example of what I mean, assume there are two rows called <code>value1</code> and <code>value2</code>, and <code>name</code> is a <code>hybrid_property</code>.</p>
  
  <pre><code># With as_scalar()
  &gt;&gt;&gt; session.query(Model).filter(Model.value=='value1').all()
  [([&lt;__main__.Model object],)]         # this is wanted
  &gt;&gt;&gt; session.query(Model.value).all()
  [(u'value1',)]
  
  # Without as scalar()
  &gt;&gt;&gt; session.query(Model).filter(Model.value=='value1').all()
  []
  &gt;&gt;&gt; session.query(Model.value).all()
  [(u'value1',), (u'value2',)]          # this is wanted
  </code></pre>
  
  <p>Depending on if <code>as_scalar()</code> is used, it changes what it works with. Is there a way to make it work with both?</p>
  
  <p>Here's some example code (edited to show an example that fully doesn't work):</p>
  
  <pre><code>import os
  from sqlalchemy import create_engine, Column, Integer, String, select, ForeignKey
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.ext.hybrid import hybrid_property
  from sqlalchemy.orm import sessionmaker, relationship
  
  Engine = create_engine('sqlite://')
  
  Base = declarative_base(bind=Engine)
  
  Session = sessionmaker(bind=Base.metadata.bind)
  
  
  class ModelRelation(Base):
      __tablename__ = 'ModelRelation'
      row_id = Column(Integer, primary_key=True)
      name = Column(String(64))
  
  
  class Model(Base):
      __tablename__ = 'Model'
      row_id = Column(Integer, primary_key=True)
      relation_id = Column(Integer, ForeignKey('ModelRelation.row_id'))
  
      relation = relationship('ModelRelation')
  
      @hybrid_property
      def value(self):
          return self.relation.name
  
      @value.expression
      def value(cls):
          return select([ModelRelation.name]).where(ModelRelation.row_id==cls.relation_id)
  
      @hybrid_property
      def value_scalar(self):
          return self.relation.name
  
      @value_scalar.expression
      def value_scalar(cls):
          return select([ModelRelation.name]).where(ModelRelation.row_id==cls.relation_id).as_scalar()
  
  
  Base.metadata.create_all()
  
  if __name__ == '__main__':
      session = Session()
  
      script1 = Model(relation=ModelRelation(name='value1'))
      session.add(script1)
      script2 = Model(relation=ModelRelation(name='value2'))
      session.add(script2)
      session.commit()
  
      print([i.value for i in session.query(Model).all()])
      print(session.query(Model.value).all())
      print(session.query(Model).filter(Model.value=='value1').all())
      print()
      print([i.value_scalar for i in session.query(Model).all()])
      print(session.query(Model.value_scalar).all())
      print(session.query(Model).filter(Model.value_scalar=='value1').all())
  
      session.close()
  </code></pre>
  
  <p>The output of that is:</p>
  
  <pre><code>[u'value1', u'value2']
  [(u'value1',), (u'value2',)]
  []
  
  [u'value1', u'value2']
  [(u'value1',)]
  [&lt;__main__.Model object at 0x041D5C90&gt;]
  </code></pre>