Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the full path to an entity with inheritance hierarchies in contains_eager()

I have a query like

(session.query(Root).with_polymorphic('*')
    .outerjoin(Subclass.related1).options(contains_eager(Subclass.related1)))

So far things work.

I want also want to eagerly load Related1.related2 and I tried this:

(session.query(Root).with_polymorphic('*')
    .outerjoin(Subclass.related1).options(contains_eager(Subclass.related1))
    .outerjoin(Related1.related2).options(contains_eager(Related1.related2)))

But it doesn't work:

sqlalchemy.exc.ArgumentError: Can't find property 'related2' on any entity specified in this Query. Note the full path from root (Mapper|Root|root) to target entity must be specified.

Given that related1 is related to the root entity via a subclass I don't see how to specify the full path.

I also tried

(session.query(Root).with_polymorphic('*')
    .outerjoin(Subclass.related1).options(contains_eager(Subclass.related1))
    .outerjoin(Related1.related2).options(contains_eager('related1.related2')))

which predictably fails with

sqlalchemy.exc.ArgumentError: Can't find property named 'related1' on the mapped entity Mapper|Root|root in this Query.

How can I specify the full path to the indirectly-related entity in contains_eager()?

like image 632
jd. Avatar asked Jan 19 '13 12:01

jd.


1 Answers

contains_eager needs a full path from the entities the query knows about:

contains_eager(Subclass.related1, Related1.related2)
like image 136
zzzeek Avatar answered Oct 16 '22 06:10

zzzeek