On Feb 11, 2013, at 5:45 PM, Matthew Rich <matt...@matthewrich.com> wrote:

> 
> And I want to achieve SQL that basically looks like this (IE, show me the 
> schools with more than one coordinator):
> SELECT s.Id, COUNT(c.Id)
> FROM School s
> INNER JOIN SchoolCoordinator sc ON s.Id = sc.SchoolId
> INNER JOIN Coordinator c ON sc.CoordinatorId = c.Id
> GROUP BY s.Id
> HAVING COUNT(c.Id) > 1
> 
> The HAVING example in the query object documentation is for a one-to-many 
> relationship and seems pretty straightforward. None of the incantations I've 
> tried have worked for a m2m relationship however. Any advice is much 
> appreciated.

should be easy enough to create those joins automatically using the 
relationship:

query(School.id, 
func.count(Coordinator.id)).join(School.coordinators).group_by(School.id).having(func.count(Coordinator.id)
 > 1)


or you could join() explicitly with a separate target/ON clause, shouldn't be 
necessary though.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to