[sqlalchemy] Re: Cached ORM instances and eagerload queries

2007-06-13 Thread Michael Bayer



On Jun 13, 4:23 pm, Cory Johns [EMAIL PROTECTED] wrote:
 I find myself in a situation where I need certain (normally lazy-loaded)
 properties of an ORM class to be eagerloaded for a particular query.  I pass
 withoptions=[eagerload('property')] in to session.query(), and everything
 works fine.  At least, it did until the query picked up a record that had
 been previously returned by another query without eagerloading.  Because the
 record was already cached in the session, it didn't pick up on the eagerload
 option.

 What I'd like to know is, is there a way to force the eagerloading query to
 apply its eagerloading behavior, even if the record is already cached in the
 session?

im assuming you mean one of the lead instances in your selection did
not get its collection updated since it was already present (as
opposed to, one of the eagerly loaded child items didnt get its
attributes refreshed).

I normally answer this along the lines of use load() to load the
instance, which reloads all of its attributes, or expire()/refresh()
an already loaded instance.  but you are trying to load a set of
instances here and theres no public hook to indicate load() behavior
for a whole list.

this is something easy enough to add in version 0.4 of query so i will
for now show you the non-public way you can accomplish this:

query= session.query(MyClass).options(..).filter_by(..)..etc..
result = query._select_statement(query.compile(),
populate_existing=True)

the effect that the above will have is to completely disregard any
attributes set on elements that are already in the session; they'll
all be overwritten...i.e. all column-based attributes, collections,
etc. all the way down for everything accessed. its like calling
refresh() on every instance.

if you need finer grained options than that, i.e.
options(overwrite_collection('foo')), that would take a lot more
tinkering under the hood.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Cached ORM instances and eagerload queries

2007-06-13 Thread Cory Johns

Excellent, thanks.  Works a treat.

-Original Message-
From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED]
Behalf Of Michael Bayer
Sent: Wednesday, June 13, 2007 4:51 PM
To: sqlalchemy
Subject: [sqlalchemy] Re: Cached ORM instances and eagerload queries


...

query= session.query(MyClass).options(..).filter_by(..)..etc..
result = query._select_statement(query.compile(),
populate_existing=True)

...




CONFIDENTIAL NOTICE: This email including any attachments, contains 
confidential information belonging to the sender. It may also be 
privileged or otherwise protected by work product immunity or other 
legal rules. This information is intended only for the use of the 
individual or entity named above.  If you are not the intended 
recipient, you are hereby notified that any disclosure, copying, 
distribution or the taking of any action in reliance on the contents 
of this emailed information is strictly prohibited.  If you have 
received this email in error, please immediately notify us by 
reply email of the error and then delete this email immediately.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---