This is on purpose and is documented here: http://docs.sqlalchemy.org/en/rel_0_9/orm/loading.html?highlight=eagerload#contains-eager
Populating relationships is separate from joining for filtering by default. To cut down on the duplicate joins, you can use query.options(contains_eager(Base.owner)
to make the join populate the relationship.
Since you say there are five joins appearing, I assume you have other relationships with lazy='joined'
, those will by definition need a join one way or another. contains_eager
just cuts down on duplicates in some situations. Another solution is to set lazy='select'
(or not have it at all) to make the relation lazy.