[sqlalchemy] Re: How to count related objects from a many to many relations using subquery

2009-09-13 Thread Mario César
On Sep 11, 5:03 pm, Conor wrote: > I believe what you want is: > q = session.query(Option, func.count()) > q = q.outerjoin((option_senators, option_senators.c.option_id == > Option.id)) > q = q.group_by(Option.id) > q = q.order_by(Option.name) > > which will generate this SQL: > SELECT [...], c

[sqlalchemy] Re: How to count related objects from a many to many relations using subquery

2009-09-11 Thread Conor
I believe what you want is: q = session.query(Option, func.count()) q = q.outerjoin((option_senators, option_senators.c.option_id == Option.id)) q = q.group_by(Option.id) q = q.order_by(Option.name) which will generate this SQL: SELECT [...], count(*) AS count_1 FROM options LEFT OUTER JOIN optio