Correlated Subquery Relationship Hybrid, , count
:
@hybrid_property
def has_a_boy_child(self):
return any(child.is_boy for child in self.children)
@has_a_boy_child.expression
def has_a_boy_child(cls):
return (
select([func.count(Child.id)])
.where(Child.parent_id == cls.id)
.where(Child.is_boy == True)
.label("number_of_boy_children")
)
:
q_has_boys = session.query(Parent).filter(Parent.has_a_boy_child).all()
q_no_boys = session.query(Parent).filter(~Parent.has_a_boy_child).all()
q_attr = session.query(Parent, Parent.has_a_boy_child).all()
. bool
count
( None
na
pandas), , :
@has_a_boy_child.expression
def has_a_boy_child(cls):
return (
select([
case([(exists().where(and_(
Child.parent_id == cls.id,
Child.is_boy == True,
)).correlate(cls), True)],
else_=False,
).label("has_boys")
])
.label("number_of_boy_children")
)