(
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],
)
| 3976 | self.autoflush = autoflush |
| 3977 | |
| 3978 | def _merge( |
| 3979 | self, |
| 3980 | state: InstanceState[_O], |
| 3981 | state_dict: _InstanceDict, |
| 3982 | *, |
| 3983 | options: Optional[Sequence[ORMOption]] = None, |
| 3984 | load: bool, |
| 3985 | _recursive: Dict[Any, object], |
| 3986 | _resolve_conflict_map: Dict[_IdentityKeyType[Any], object], |
| 3987 | ) -> _O: |
| 3988 | mapper: Mapper[_O] = _state_mapper(state) |
| 3989 | if state in _recursive: |
| 3990 | return cast(_O, _recursive[state]) |
| 3991 | |
| 3992 | new_instance = False |
| 3993 | key = state.key |
| 3994 | |
| 3995 | merged: Optional[_O] |
| 3996 | |
| 3997 | if key is None: |
| 3998 | if state in self._new: |
| 3999 | util.warn( |
| 4000 | "Instance %s is already pending in this Session yet is " |
| 4001 | "being merged again; this is probably not what you want " |
| 4002 | "to do" % state_str(state) |
| 4003 | ) |
| 4004 | |
| 4005 | if not load: |
| 4006 | raise sa_exc.InvalidRequestError( |
| 4007 | "merge() with load=False option does not support " |
| 4008 | "objects transient (i.e. unpersisted) objects. flush() " |
| 4009 | "all changes on mapped instances before merging with " |
| 4010 | "load=False." |
| 4011 | ) |
| 4012 | key = mapper._identity_key_from_state(state) |
| 4013 | key_is_persistent = LoaderCallableStatus.NEVER_SET not in key[ |
| 4014 | 1 |
| 4015 | ] and ( |
| 4016 | not _none_set.intersection(key[1]) |
| 4017 | or ( |
| 4018 | mapper.allow_partial_pks |
| 4019 | and not _none_set.issuperset(key[1]) |
| 4020 | ) |
| 4021 | ) |
| 4022 | else: |
| 4023 | key_is_persistent = True |
| 4024 | |
| 4025 | merged = self.identity_map.get(key) |
| 4026 | |
| 4027 | if merged is None: |
| 4028 | if key_is_persistent and key in _resolve_conflict_map: |
| 4029 | merged = cast(_O, _resolve_conflict_map[key]) |
| 4030 | |
| 4031 | elif not load: |
| 4032 | if state.modified: |
| 4033 | raise sa_exc.InvalidRequestError( |
| 4034 | "merge() with load=False option does not support " |
| 4035 | "objects marked as 'dirty'. flush() all changes on " |
no test coverage detected