From: Federico Caselli Date: Thu, 9 Dec 2021 21:11:37 +0000 (+0100) Subject: Add execution options to ``Session.get`` X-Git-Tag: rel_1_4_29~12^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fd7a0e60d0e9df81f44271a65c8364408e98235e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add execution options to ``Session.get`` Fixes: #7410 Change-Id: Iab6427b8b4c2ada8c31ef69f92d27c1185dbb6b1 (cherry picked from commit ec1fee363e7d538a2239818cfb1e341eddddcf36) --- diff --git a/doc/build/changelog/unreleased_14/7410.rst b/doc/build/changelog/unreleased_14/7410.rst new file mode 100644 index 0000000000..7b4e8efbf1 --- /dev/null +++ b/doc/build/changelog/unreleased_14/7410.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: usecase, orm + :tickets: 7410 + + Added :paramref:`_orm.Session.get.execution_options` parameter which was + previously missing from the :meth:`_orm.Session.get` method. diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 034651326b..5f1560a697 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2685,6 +2685,7 @@ class Session(_SessionClassMethods): populate_existing=False, with_for_update=None, identity_token=None, + execution_options=None, ): """Return an instance based on the given primary key identifier, or ``None`` if not found. @@ -2765,6 +2766,19 @@ class Session(_SessionClassMethods): :meth:`_query.Query.with_for_update`. Supersedes the :paramref:`.Session.refresh.lockmode` parameter. + :param execution_options: optional dictionary of execution options, + which will be associated with the query execution if one is emitted. + This dictionary can provide a subset of the options that are + accepted by :meth:`_engine.Connection.execution_options`, and may + also provide additional options understood only in an ORM context. + + .. versionadded:: 1.4.29 + + .. seealso:: + + :ref:`orm_queryguide_execution_options` - ORM-specific execution + options + :return: The object instance, or ``None``. """ @@ -2776,6 +2790,7 @@ class Session(_SessionClassMethods): populate_existing=populate_existing, with_for_update=with_for_update, identity_token=identity_token, + execution_options=execution_options, ) def _get_impl( diff --git a/test/orm/test_scoping.py b/test/orm/test_scoping.py index 87f0a2aae8..e23f42ac53 100644 --- a/test/orm/test_scoping.py +++ b/test/orm/test_scoping.py @@ -156,6 +156,7 @@ class ScopedSessionTest(fixtures.MappedTest): populate_existing=False, with_for_update=None, identity_token=None, + execution_options=None, ), ], ) diff --git a/test/orm/test_session.py b/test/orm/test_session.py index 4ee71fd5ba..62974b6291 100644 --- a/test/orm/test_session.py +++ b/test/orm/test_session.py @@ -461,6 +461,23 @@ class SessionUtilTest(_fixtures.FixtureTest): u1, ) + def test_get_execution_option(self): + users, User = self.tables.users, self.classes.User + + self.mapper_registry.map_imperatively(User, users) + sess = fixture_session() + called = [False] + + @event.listens_for(sess, "do_orm_execute") + def check(ctx): + called[0] = True + eq_(ctx.execution_options["foo"], "bar") + + sess.get(User, 42, execution_options={"foo": "bar"}) + sess.close() + + is_true(called[0]) + class SessionStateTest(_fixtures.FixtureTest): run_inserts = None