Sqlalchemy: Get child count without reading all the children
I think you unnecessary iterate over children within active_count
hybrid_property definition. This should works for you:
class Parent(Base):
...
children = relationship("Child", backref="parent", lazy="dynamic")
@hybrid_property
def active_count(self):
return self.children.filter_by(active=True).with_entities(func.count('*')).scalar()
# or
# return self.children.filter_by(active=True).count()
# but it would have worse performance