On Jul 12, 2008, at 1:57 PM, Kyle Schaffrick wrote:

>
> I can't figure out any good way to phrase my question without some
> pipe-laying, so here I go...
>
> I have a pattern of doing some various query activities in my app that
> ultimately result in retrieving a single instance of one of my mapped
> classes from the ORM. So naturally I stuck that pattern in a function,
> which I pass my session object, a class, and some other parameters and
> it either hands me an instance or raises exceptions. The point here is
> that I never see a Query object if I use this function.
>
> So, now suppose I have my instance (call it "obj") and it has a 1-M
> relation, obj.rel, to a class Secondary. Secondary is in turn related
> (1-1 in this case) to Tertiary.
>
> If I start iterating over the obj.rel collection and access the  
> Tertiary
> objects, I get O(n) behavior as one would expect. Is it possible to  
> set
> up an eager-load of the Tertiary relation from *here*, after the Query
> is long gone? I would love to avoid tangling "should I eager-load or
> not" logic into the instance-fetching function, just for these one or
> two cases.

You can do it when the Query first happens, but have the eagerload  
operation delayed until the lazyload.   the "eagerload()" query option  
will plant itself inside the lazy loader and take effect when the lazy  
load occurs, if that's how its set up.

assume A->bs->B->cs->C

a = sess.query(A).options(eagerload('cs')).first()
print a.bs[0].cs  # will lazyload "b" eagerloading "cs"

This same idea takes effect if you just say "lazy=False" when you  
declare the "cs" relation on "B".

To setup the eagerness of "cs" at the exact point of lazy load, the  
public API that allows this is via "lazy='dynamic'", which would allow:

a = sess.query(A).first()
print a.bs.option(eagerload('cs')).first()

But that changes the "lazyness" of "bs" considerably.

Yet another option would be an API that allows the "eagerload('cs')"  
option to be embedded into the lazy loader present on "A.bs".  This is  
possible right now but you'd be calling upon non-public APIs to do it.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to