Python SQLAlchemy按子级数多对多筛选行
检索具有多个子表(多对多表的一部分)中所有行的最佳方法是什么?我试过:Python SQLAlchemy按子级数多对多筛选行,python,sql,sqlalchemy,many-to-many,Python,Sql,Sqlalchemy,Many To Many,检索具有多个子表(多对多表的一部分)中所有行的最佳方法是什么?我试过: session.query(Parent).filter(len(Parent.children)>1).all() 但我得到的错误是“InstrumentedAttribute类型的对象”没有len()。我已经能够让所有至少有一个孩子的父母使用: session.query(Parent).filter(Parent.children).all() 使用 from sqlalchemy import func
session.query(Parent).filter(len(Parent.children)>1).all()
但我得到的错误是“InstrumentedAttribute类型的对象”没有len()。我已经能够让所有至少有一个孩子的父母使用:
session.query(Parent).filter(Parent.children).all()
使用
from sqlalchemy import func
session.query(Parent).\
join(Parent.children).\
group_by(Parent).\
having(func.count(Child.id) > 1)