diff options
Diffstat (limited to 'lib/sqlalchemy/orm/session.py')
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index d8af4e74f..954b31310 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -1141,8 +1141,7 @@ class Session(object): for state, m, o in cascade_states: self._delete_impl(state) - def merge(self, instance, dont_load=False, - _recursive=None): + def merge(self, instance, dont_load=False): """Copy the state an instance onto the persistent instance with the same identifier. If there is no persistent instance currently associated with the @@ -1155,13 +1154,18 @@ class Session(object): mapped with ``cascade="merge"``. """ - if _recursive is None: - # TODO: this should be an IdentityDict for instances, but will - # need a separate dict for PropertyLoader tuples - _recursive = {} - # Autoflush only on the topmost call - self._autoflush() - + # TODO: this should be an IdentityDict for instances, but will + # need a separate dict for PropertyLoader tuples + _recursive = {} + self._autoflush() + autoflush = self.autoflush + try: + self.autoflush = False + return self._merge(instance, dont_load=dont_load, _recursive=_recursive) + finally: + self.autoflush = autoflush + + def _merge(self, instance, dont_load=False, _recursive=None): mapper = _object_mapper(instance) if instance in _recursive: return _recursive[instance] @@ -1169,6 +1173,7 @@ class Session(object): new_instance = False state = attributes.instance_state(instance) key = state.key + if key is None: if dont_load: raise sa_exc.InvalidRequestError( @@ -1194,7 +1199,7 @@ class Session(object): self._update_impl(merged_state) new_instance = True else: - merged = self.query(mapper.class_).autoflush(False).get(key[1]) + merged = self.query(mapper.class_).get(key[1]) if merged is None: merged = mapper.class_manager.new_instance() |