def merge(self, session, source, dest, dont_load, _recursive): value = attributes.get_as_list(source._state, self.key, passive=True) if value: setattr(dest, self.key, value[0]) else: # TODO: lazy callable should merge to the new instance dest._state.expire_attributes([self.key])
def merge(self, session, source, dest, dont_load, _recursive): if not dont_load and self._reverse_property and (source, self._reverse_property) in _recursive: return if not "merge" in self.cascade: dest._state.expire_attributes([self.key]) return instances = attributes.get_as_list(source._state, self.key, passive=True) if not instances: return if self.uselist: dest_list = [] for current in instances: _recursive[(current, self)] = True obj = session.merge(current, entity_name=self.mapper.entity_name, dont_load=dont_load, _recursive=_recursive) if obj is not None: dest_list.append(obj) if dont_load: coll = attributes.init_collection(dest, self.key) for c in dest_list: coll.append_without_event(c) else: getattr(dest.__class__, self.key).impl._set_iterable(dest._state, dest_list) else: current = instances[0] if current is not None: _recursive[(current, self)] = True obj = session.merge(current, entity_name=self.mapper.entity_name, dont_load=dont_load, _recursive=_recursive) if obj is not None: if dont_load: dest.__dict__[self.key] = obj else: setattr(dest, self.key, obj)
def cascade_iterator(self, type_, state, visited_instances, halt_on=None): if not type_ in self.cascade: return passive = type_ != 'delete' or self.passive_deletes mapper = self.mapper.primary_mapper() instances = attributes.get_as_list(state, self.key, passive=passive) if instances: for c in instances: if c is not None and c not in visited_instances and (halt_on is None or not halt_on(c)): if not isinstance(c, self.mapper.class_): raise exceptions.AssertionError("Attribute '%s' on class '%s' doesn't handle objects of type '%s'" % (self.key, str(self.parent.class_), str(c.__class__))) visited_instances.add(c) # cascade using the mapper local to this object, so that its individual properties are located instance_mapper = object_mapper(c, entity_name=mapper.entity_name) yield (c, instance_mapper, c._state)