MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _merge

Method _merge

lib/sqlalchemy/orm/session.py:4134–4291  ·  view source on GitHub ↗
(
        self,
        state: InstanceState[_O],
        state_dict: _InstanceDict,
        *,
        options: Optional[Sequence[ORMOption]] = None,
        load: bool,
        _recursive: Dict[Any, object],
        _resolve_conflict_map: Dict[_IdentityKeyType[Any], object],
    )

Source from the content-addressed store, hash-verified

4132 ]
4133
4134 def _merge(
4135 self,
4136 state: InstanceState[_O],
4137 state_dict: _InstanceDict,
4138 *,
4139 options: Optional[Sequence[ORMOption]] = None,
4140 load: bool,
4141 _recursive: Dict[Any, object],
4142 _resolve_conflict_map: Dict[_IdentityKeyType[Any], object],
4143 ) -> _O:
4144 mapper: Mapper[_O] = _state_mapper(state)
4145 if state in _recursive:
4146 return cast(_O, _recursive[state])
4147
4148 new_instance = False
4149 key = state.key
4150
4151 merged: Optional[_O]
4152
4153 if key is None:
4154 if state in self._new:
4155 util.warn(
4156 "Instance %s is already pending in this Session yet is "
4157 "being merged again; this is probably not what you want "
4158 "to do" % state_str(state)
4159 )
4160
4161 if not load:
4162 raise sa_exc.InvalidRequestError(
4163 "merge() with load=False option does not support "
4164 "objects transient (i.e. unpersisted) objects. flush() "
4165 "all changes on mapped instances before merging with "
4166 "load=False."
4167 )
4168 key = mapper._identity_key_from_state(state)
4169 key_is_persistent = LoaderCallableStatus.NEVER_SET not in key[
4170 1
4171 ] and (
4172 not _none_set.intersection(key[1])
4173 or (
4174 mapper.allow_partial_pks
4175 and not _none_set.issuperset(key[1])
4176 )
4177 )
4178 else:
4179 key_is_persistent = True
4180
4181 merged = self.identity_map.get(key)
4182
4183 if merged is None:
4184 if key_is_persistent and key in _resolve_conflict_map:
4185 merged = cast(_O, _resolve_conflict_map[key])
4186
4187 elif not load:
4188 if state.modified:
4189 raise sa_exc.InvalidRequestError(
4190 "merge() with load=False option does not support "
4191 "objects marked as 'dirty'. flush() all changes on "

Callers 7

mergeMethod · 0.95
merge_allMethod · 0.95
merge_with_historyMethod · 0.45
mergeMethod · 0.45
merge_frozen_resultFunction · 0.45
merge_resultFunction · 0.45

Calls 15

getMethod · 0.95
_update_implMethod · 0.95
_save_or_update_stateMethod · 0.95
_state_mapperFunction · 0.85
state_strFunction · 0.85
_copy_callablesMethod · 0.80
_commit_allMethod · 0.80
castFunction · 0.50
warnMethod · 0.45

Tested by

no test coverage detected