diff options
author | Daniel Stone <me@danstone.uk> | 2021-08-30 11:15:25 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-09-02 15:18:23 -0400 |
commit | 22300dbd24f2152636491d2287bee08935e14282 (patch) | |
tree | 627a229a2c4cf707aeeeeaa30ab9c3296365d00e /lib/sqlalchemy/orm/session.py | |
parent | d640192877e4d1da75e8dea34d2374c404e80538 (diff) | |
download | sqlalchemy-22300dbd24f2152636491d2287bee08935e14282.tar.gz |
Added loader options to session.merge, asyncsession.merge
Added loader options to :meth:`_orm.Session.merge` and
:meth:`_asyncio.AsyncSession.merge`, which will apply the given loader
options to the ``get()`` used internally by merge, allowing eager loading
of relationships etc. to be applied when the merge process loads a new
object. Pull request courtesy Daniel Stone.
Fixes: #6955
Closes: #6957
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6957
Pull-request-sha: ab4d96cd5da9a5dd01112b8dcd6514db64aa8d9f
Change-Id: I5b94dfda1088a8bc6396e9fd9a072827df1f8680
Diffstat (limited to 'lib/sqlalchemy/orm/session.py')
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 0bdd5cc95..a93684126 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2843,7 +2843,7 @@ class Session(_SessionClassMethods): load_options=load_options, ) - def merge(self, instance, load=True): + def merge(self, instance, load=True, options=None): """Copy the state of a given instance into a corresponding instance within this :class:`.Session`. @@ -2889,6 +2889,11 @@ class Session(_SessionClassMethods): produced as "clean", so it is only appropriate that the given objects should be "clean" as well, else this suggests a mis-use of the method. + :param options: optional sequence of loader options which will be + applied to the :meth:`_orm.Session.get` method when the merge + operation loads the existing version of the object from the database. + + .. versionadded:: 1.4.24 .. seealso:: @@ -2916,6 +2921,7 @@ class Session(_SessionClassMethods): attributes.instance_state(instance), attributes.instance_dict(instance), load=load, + options=options, _recursive=_recursive, _resolve_conflict_map=_resolve_conflict_map, ) @@ -2927,6 +2933,7 @@ class Session(_SessionClassMethods): state, state_dict, load=True, + options=None, _recursive=None, _resolve_conflict_map=None, ): @@ -2990,7 +2997,12 @@ class Session(_SessionClassMethods): new_instance = True elif key_is_persistent: - merged = self.get(mapper.class_, key[1], identity_token=key[2]) + merged = self.get( + mapper.class_, + key[1], + identity_token=key[2], + options=options, + ) if merged is None: merged = mapper.class_manager.new_instance() |