Along those same lines, one of the things I would like to do is to do an
arbitrary join, my table is hierarchical and often I want to do a join
against a a node, along with another node two levels deep below it, and
since its a tree structure eager loading all of the children, and
grandchildren would be costly, when I only care about two of them.
So I have this:
scn_alias=aliased(Asset)
p='testshow/eps/201/s22/t04'
take = session.query(Asset).filter(Asset.path==p).\
outerjoin((scn_alias,scn_alias.path=="%s/anim/scn"%p),aliased=True).one()
This produces the correct SQL, but since scn_alias isn't directly mapped
as a relation to the row I am querying, I can't figure out the correct
arguments to pass to contains_eager().
Normally I would query for the grandchild and
eagerload_all('Parent.Parent'), but in this case the grandchild might
not exist.
Michael Bayer wrote:
> David Gardner wrote:
>
>> I'm having a bit of a problem eager loading the parent node of a
>> hierarchical tree-like table (ie, every node has one to many children).
>> If I simply add a "options(eagerload(Asset.Parent))" to my query it
>> works as expected.
>>
>> However often I need to select a node based on it's attributes as well
>> as the parent's attributes, so do a
>> "join(Asset.Parent).options(contains_eager(Asset.Parent))"
>>
>
> if this is a self referential join, you have to alias the target you're
> joining to. usually the aliased=True flag would be sufficient for the
> join(), but since you want to contains_eager() it as well, this all must
> be laid out explicitly:
>
>
> parent = aliased(Asset)
>
> query.join((parent, Asset.parent)).options(contains_eager(Asset.parent,
> alias=parent))
>
>
> >
>
>
--
David Gardner
Pipeline Tools Programmer, "Sid the Science Kid"
Jim Henson Creature Shop
[email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---