When I do a query like this
result = session.query(A).\
join(A.bs).\
join(B.cs).\
filter_by(C.somedata.in_([4455, 4466])).\
options(contains_eager(A.bs).contains_eager(B.cs)).one()
and list related C objects
for b in result.bs:
b.cs
I get all related to B C objects instead of only those with 4455, 4466 values - even though I filtered the output of the statement.
So eager loading when used with contains_eager() - as I understand - shouldn't load other related objects apart from those which are selected by statement. Still I get Cs with values other that 4455, 4466(i.e. all the collection) :( Could any suggest from where come other Cs? Thanks in advance
populate_existing()
Anyway, yes, contains_eager says, “populate these collections”. the usage looks correct, so the echo=True, or even echo=‘debug’ which will show you the result rows coming in, will show you where its going wrong.
or if those collections were already populated previously within that Session, that would affect it also. you can say query.populate_existing() to force it to re-load everything.