I've been trying to google this but I'm only really coming up with how to retrieve the count rather than filter by it. The closest result I found was this answer, but I'm constructing the query without a session so using object_session
raises UnmappedInstanceError
.
Given Parent
and Child
models, connected by Parent.children
, how could I query which parents have a certain amount of children?
I've tried session.query(Parent).filter(func.count(Parent.children)>1)
, but it complains about misuse of the count
function.
As I'm building the query recursively for a search function, the query is actually built up of dozens of filters, so if at all possible I'd like this to remain in a single filter.