MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / merge

Method merge

lib/sqlalchemy/orm/session.py:4017–4096  ·  view source on GitHub ↗

Copy the state of a given instance into a corresponding instance within this :class:`.Session`. :meth:`.Session.merge` examines the primary key attributes of the source instance, and attempts to reconcile it with an instance of the same primary key in the session.

(
        self,
        instance: _O,
        *,
        load: bool = True,
        options: Optional[Sequence[ORMOption]] = None,
    )

Source from the content-addressed store, hash-verified

4015 )
4016
4017 def merge(
4018 self,
4019 instance: _O,
4020 *,
4021 load: bool = True,
4022 options: Optional[Sequence[ORMOption]] = None,
4023 ) -> _O:
4024 """Copy the state of a given instance into a corresponding instance
4025 within this :class:`.Session`.
4026
4027 :meth:`.Session.merge` examines the primary key attributes of the
4028 source instance, and attempts to reconcile it with an instance of the
4029 same primary key in the session. If not found locally, it attempts
4030 to load the object from the database based on primary key, and if
4031 none can be located, creates a new instance. The state of each
4032 attribute on the source instance is then copied to the target
4033 instance. The resulting target instance is then returned by the
4034 method; the original source instance is left unmodified, and
4035 un-associated with the :class:`.Session` if not already.
4036
4037 This operation cascades to associated instances if the association is
4038 mapped with ``cascade="merge"``.
4039
4040 See :ref:`unitofwork_merging` for a detailed discussion of merging.
4041
4042 :param instance: Instance to be merged.
4043 :param load: Boolean, when False, :meth:`.merge` switches into
4044 a "high performance" mode which causes it to forego emitting history
4045 events as well as all database access. This flag is used for
4046 cases such as transferring graphs of objects into a :class:`.Session`
4047 from a second level cache, or to transfer just-loaded objects
4048 into the :class:`.Session` owned by a worker thread or process
4049 without re-querying the database.
4050
4051 The ``load=False`` use case adds the caveat that the given
4052 object has to be in a "clean" state, that is, has no pending changes
4053 to be flushed - even if the incoming object is detached from any
4054 :class:`.Session`. This is so that when
4055 the merge operation populates local attributes and
4056 cascades to related objects and
4057 collections, the values can be "stamped" onto the
4058 target object as is, without generating any history or attribute
4059 events, and without the need to reconcile the incoming data with
4060 any existing related objects or collections that might not
4061 be loaded. The resulting objects from ``load=False`` are always
4062 produced as "clean", so it is only appropriate that the given objects
4063 should be "clean" as well, else this suggests a mis-use of the
4064 method.
4065 :param options: optional sequence of loader options which will be
4066 applied to the :meth:`_orm.Session.get` method when the merge
4067 operation loads the existing version of the object from the database.
4068
4069 .. versionadded:: 1.4.24
4070
4071
4072 .. seealso::
4073
4074 :func:`.make_transient_to_detached` - provides for an alternative

Callers 15

_mergeMethod · 0.45
test_merge_resultsMethod · 0.45
test_closeMethod · 0.45
test_fetchallMethod · 0.45
test_firstMethod · 0.45
test_columnsMethod · 0.45
scoped_session.pyFile · 0.45
test_merge_resultsMethod · 0.45
test_fetchallMethod · 0.45
test_firstMethod · 0.45

Calls 4

_flush_warningMethod · 0.95
_autoflushMethod · 0.95
_mergeMethod · 0.95
object_stateFunction · 0.85

Tested by 15

test_merge_resultsMethod · 0.36
test_closeMethod · 0.36
test_fetchallMethod · 0.36
test_firstMethod · 0.36
test_columnsMethod · 0.36
test_merge_resultsMethod · 0.36
test_fetchallMethod · 0.36
test_firstMethod · 0.36
test_columnsMethod · 0.36
test_merge_scalarsMethod · 0.36