Thanks a lot Mike, for the little advice and for your big work! And here's the working code for the people who comes to this page later.
categories = db.session.query(Categories).filter(Categories.parent_id == None, Categories.site_id == 1).\ outerjoin(child, Categories.children).filter(Categories.status == 'ACTIVE', child.status == 'ACTIVE').\ options(contains_eager(Categories.children.of_type(child))).all() -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- 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 view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/5c08c65d-34d2-4293-bf83-820c718716ec%40googlegroups.com.