2015-12-09 89 views
2

什么是最好的方式检索一个表中的所有行(这是多对多的一部分)与多个孩子?我曾尝试过:SQLAlchemy多对多筛选儿童人数

session.query(Parent).filter(len(Parent.children)>1).all() 

但我得到错误'对象类型'InstrumentedAttribute'没有len()'。我已经能够得到所有家长使用至少有一个孩子:

session.query(Parent).filter(Parent.children).all() 

回答

4

使用having()

from sqlalchemy import func 

session.query(Parent).\ 
     join(Parent.children).\ 
     group_by(Parent).\ 
     having(func.count(Child.id) > 1)