SQLAlchemy - How can I make eager loading count property

后端 未结 2 992
北荒
北荒 2021-02-15 02:11

I want to make a property for model which contains count.

Since I always need the property, I want to make query with JOIN like sqlalchemy.orm.relatio

2条回答
  •  再見小時候
    2021-02-15 02:54

    Please take a look at the Hybrid Attribute extension.

    Your object model will look similar to the below:

    class Foo(Base):
        __tablename__ = 'foo'
        id = Column(Integer, primary_key=True)
        bar_id = Column(Integer, ForeignKey('bar.id'))
        bar = relationship('Bar')
    
    class Bar(Base):
        __tablename__ = 'bar'
        id = Column(Integer, primary_key=True)
    
        @hybrid_property
        def foo_count(self):
            return object_session(self).query(Foo).filter(Foo.bar==self).count()
    
        @foo_count.expression
        def foo_count(cls):
            return select([func.count(Foo.id)]).where(Foo.bar_id == cls.id).label('foo_count')
    

    foo_count will not be eagerly loaded, but you can use it in queries like below (both in SELECT and in WHERE clause:

    qry = session.query(Bar, Bar.foo_count).filter(Bar.foo_count > 0)
    for (bar, bar_foo_count) in qry:
        print bar, bar_foo_count
    

    As you can see, the query will return tuples of (Bar, foo_count) in just one query, and now you can do what you wish with that.

提交回复
热议问题