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,
)
| 3889 | ) |
| 3890 | |
| 3891 | def merge( |
| 3892 | self, |
| 3893 | instance: _O, |
| 3894 | *, |
| 3895 | load: bool = True, |
| 3896 | options: Optional[Sequence[ORMOption]] = None, |
| 3897 | ) -> _O: |
| 3898 | """Copy the state of a given instance into a corresponding instance |
| 3899 | within this :class:`.Session`. |
| 3900 | |
| 3901 | :meth:`.Session.merge` examines the primary key attributes of the |
| 3902 | source instance, and attempts to reconcile it with an instance of the |
| 3903 | same primary key in the session. If not found locally, it attempts |
| 3904 | to load the object from the database based on primary key, and if |
| 3905 | none can be located, creates a new instance. The state of each |
| 3906 | attribute on the source instance is then copied to the target |
| 3907 | instance. The resulting target instance is then returned by the |
| 3908 | method; the original source instance is left unmodified, and |
| 3909 | un-associated with the :class:`.Session` if not already. |
| 3910 | |
| 3911 | This operation cascades to associated instances if the association is |
| 3912 | mapped with ``cascade="merge"``. |
| 3913 | |
| 3914 | See :ref:`unitofwork_merging` for a detailed discussion of merging. |
| 3915 | |
| 3916 | :param instance: Instance to be merged. |
| 3917 | :param load: Boolean, when False, :meth:`.merge` switches into |
| 3918 | a "high performance" mode which causes it to forego emitting history |
| 3919 | events as well as all database access. This flag is used for |
| 3920 | cases such as transferring graphs of objects into a :class:`.Session` |
| 3921 | from a second level cache, or to transfer just-loaded objects |
| 3922 | into the :class:`.Session` owned by a worker thread or process |
| 3923 | without re-querying the database. |
| 3924 | |
| 3925 | The ``load=False`` use case adds the caveat that the given |
| 3926 | object has to be in a "clean" state, that is, has no pending changes |
| 3927 | to be flushed - even if the incoming object is detached from any |
| 3928 | :class:`.Session`. This is so that when |
| 3929 | the merge operation populates local attributes and |
| 3930 | cascades to related objects and |
| 3931 | collections, the values can be "stamped" onto the |
| 3932 | target object as is, without generating any history or attribute |
| 3933 | events, and without the need to reconcile the incoming data with |
| 3934 | any existing related objects or collections that might not |
| 3935 | be loaded. The resulting objects from ``load=False`` are always |
| 3936 | produced as "clean", so it is only appropriate that the given objects |
| 3937 | should be "clean" as well, else this suggests a mis-use of the |
| 3938 | method. |
| 3939 | :param options: optional sequence of loader options which will be |
| 3940 | applied to the :meth:`_orm.Session.get` method when the merge |
| 3941 | operation loads the existing version of the object from the database. |
| 3942 | |
| 3943 | .. versionadded:: 1.4.24 |
| 3944 | |
| 3945 | |
| 3946 | .. seealso:: |
| 3947 | |
| 3948 | :func:`.make_transient_to_detached` - provides for an alternative |