I have a one-to-one relationship to a table in a different database
which I'd like to, effectively, eager-load. Lazy='joined' and
lazy='subquery' won't work, since they rely an impossible JOIN between
tables in two different databases.
Lazy='immediate' works, but results in one query per instance. If I
have a one-to-one relationship between A and B, and execute a query
which loads 100 A's, there will be 100 more queries which each load
one B. What I'd like is to have two queries total: one to load the As
and a second to load the Bs.
I've been able to do this manually by doing something like this:
# Do some query
parents = session.query(Parent).filter(some_condition).all()
# "Eager load"
ids = [ parent.id for parent in parents
if 'child' in instance_state(parent).unloaded ]
get_parent = session.query(Parent).get
for child in session.query(Child).filter(Child.parent_id.in_(ids)):
parent = get_parent(child.parent_id)
set_committed_value(child, 'parent', parent)
set_committed_value(parent, 'child', child)
Is there a good way to do this more automatically?
--
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.