ChildForum = aliased(Forum)
q = query(Forum, ChildForum, func.count(Message.id)).filter(Forum.parent ==
    None).outerjoin((ChildForum, Forum.children)).outerjoin(ChildForum.posts
    ).group_by(Forum, ChildForum)
How to return the count of related entities in sqlalchemy query