On 2 Apr 2014, at 20:53, Jonathan Vanasco <[email protected]> wrote:
> I'm calling this "eagerloading" , but it's not the right term...
>
> I have an object that is already loaded...
>
> user = dbSession.query( model.User ).filter_by( id = 1 ).first()
>
> over time it's seen a handful of relationships grow...
>
> user = dbSession.query( model.User ).filter_by( id = 1 )\
> .options(\
> joinedload('foo') ,
> joinedload('foo.bar') ,
> joinedload('biz') ,
> joinedload('biz.bash') ,
> )\
>
> profiling some code, I don't need to eagerload everything anymore.
>
> I'd like to try and select the least amount of data at first, then select
> more as needed.
>
> i know for single-level items I can just hit the property as `User.foo` and
> for collections i can do `list( User.foo )`
>
> but are there good ways to handle relationships like `User.foo.bar` ?
>
> My goal is to load all the data for the named relationships at once. I don't
> want to iterate through rows or leave a db cursor open.
>
One way to do it might simply be to re-query for the object, with the new eager
load options. As far as I can tell from a quick experiment, this will populate
the attributes that you ask to be eagerly loaded without touching the others.
ie.
user = dbSession.query(model.User).get(1)
# later...
# (note: must not use .get(), to skip the identity map)
user = (dbSession.query(model.User)
.filter_by(id=1)
.options(joinedload('foo'),
joinedload('foo.bar'))
.first())
This will join to the User table and fetch all its columns - I don't know if
there's an easy way to avoid that.
Simon
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.