summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-04-05 13:09:24 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-04-05 13:09:24 -0400
commitcfe330a32a6c6a59dcf4d7909eeb2e918224e8bf (patch)
tree2bda60161343daa51f7ebd778acc8aad1aaefcdc /lib
parentdbc582a43fb8e5d3c0b05cb62b6a77c4e5bc27ac (diff)
downloadsqlalchemy-cfe330a32a6c6a59dcf4d7909eeb2e918224e8bf.tar.gz
clarify intent and reduce lookups here
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/orm/dependency.py36
1 files changed, 20 insertions, 16 deletions
diff --git a/lib/sqlalchemy/orm/dependency.py b/lib/sqlalchemy/orm/dependency.py
index cbbfb0883..24c3772ee 100644
--- a/lib/sqlalchemy/orm/dependency.py
+++ b/lib/sqlalchemy/orm/dependency.py
@@ -326,22 +326,26 @@ class DetectKeySwitch(DependencyProcessor):
def _process_key_switches(self, deplist, uowcommit):
switchers = set(s for s in deplist if self._pks_changed(uowcommit, s))
if switchers:
- # yes, we're doing a linear search right now through the UOW. only
- # takes effect when primary key values have actually changed.
- # a possible optimization might be to enhance the "hasparents" capability of
- # attributes to actually store all parent references, but this introduces
- # more complicated attribute accounting.
- for s in [elem for elem in uowcommit.session.identity_map.all_states()
- if issubclass(elem.class_, self.parent.class_) and
- self.key in elem.dict and
- elem.dict[self.key] is not None and
- attributes.instance_state(elem.dict[self.key]) in switchers
- ]:
- uowcommit.register_object(s)
- sync.populate(
- attributes.instance_state(s.dict[self.key]),
- self.mapper, s, self.parent, self.prop.synchronize_pairs,
- uowcommit, self.passive_updates)
+ # if primary key values have actually changed somewhere, perform
+ # a linear search through the UOW in search of a parent.
+ # possible optimizations here include additional accounting within
+ # the attribute system, or allowing a one-to-many attr to circumvent
+ # the need for the search in this direction.
+ for state in uowcommit.session.identity_map.all_states():
+ if not issubclass(state.class_, self.parent.class_):
+ continue
+ obj = state.obj()
+ dict_ = attributes.instance_dict(obj)
+ related = dict_.get(self.key)
+ if related is not None:
+ related_state = attributes.instance_state(dict_[self.key])
+ if related_state in switchers:
+ uowcommit.register_object(state)
+ sync.populate(
+ related_state,
+ self.mapper, state,
+ self.parent, self.prop.synchronize_pairs,
+ uowcommit, self.passive_updates)
def _pks_changed(self, uowcommit, state):
return sync.source_modified(uowcommit, state, self.mapper, self.prop.synchronize_pairs)