Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy按子级数多对多筛选行_Python_Sql_Sqlalchemy_Many To Many - Fatal编程技术网

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)