]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add execution options to ``Session.get``
authorFederico Caselli <cfederico87@gmail.com>
Thu, 9 Dec 2021 21:11:37 +0000 (22:11 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Fri, 10 Dec 2021 14:35:36 +0000 (15:35 +0100)
Fixes: #7410
Change-Id: Iab6427b8b4c2ada8c31ef69f92d27c1185dbb6b1
(cherry picked from commit ec1fee363e7d538a2239818cfb1e341eddddcf36)

doc/build/changelog/unreleased_14/7410.rst [new file with mode: 0644]
lib/sqlalchemy/orm/session.py
test/orm/test_scoping.py
test/orm/test_session.py

diff --git a/doc/build/changelog/unreleased_14/7410.rst b/doc/build/changelog/unreleased_14/7410.rst
new file mode 100644 (file)
index 0000000..7b4e8ef
--- /dev/null
@@ -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.
index 034651326be061525d1f0ce45381ad5c6270a0a9..5f1560a69748a86a60a3a719addedd21dac742da 100644 (file)
@@ -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(
index 87f0a2aae897d003a5ca7757cf90fb8d8a2163ef..e23f42ac53ee980e1a99c6522c9f19f0a61a923c 100644 (file)
@@ -156,6 +156,7 @@ class ScopedSessionTest(fixtures.MappedTest):
                     populate_existing=False,
                     with_for_update=None,
                     identity_token=None,
+                    execution_options=None,
                 ),
             ],
         )
index 4ee71fd5ba58a0725e419d003611480ebe6c1437..62974b62919aaed52d1409fa4d1980480ee6eaef 100644 (file)
@@ -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