]> 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:33:50 +0000 (15:33 +0100)
Fixes: #7410
Change-Id: Iab6427b8b4c2ada8c31ef69f92d27c1185dbb6b1

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 f75f2ac9fd31ac0160d26dbebcdae9bf2f83bca8..e921bb8f0eee7372a2da50286590901f65f744ef 100644 (file)
@@ -2564,6 +2564,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.
@@ -2644,6 +2645,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``.
 
         """
@@ -2655,6 +2669,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 c5458f5ced48a11b4230839258e64c9b72c56ed6..9b255bf5e6b152089e8cfa8455620c89f7346d19 100644 (file)
@@ -157,6 +157,7 @@ class ScopedSessionTest(fixtures.MappedTest):
                     populate_existing=False,
                     with_for_update=None,
                     identity_token=None,
+                    execution_options=None,
                 ),
             ],
         )
index 1b92da24833bd8d5ddeb48cf20f8f8c5eb79861c..e821a7c20d13efa38a53fc900eb1fdf4d29eef57 100644 (file)
@@ -461,6 +461,24 @@ 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):
+            nonlocal called
+            called = True
+            eq_(ctx.execution_options["foo"], "bar")
+
+        sess.get(User, 42, execution_options={"foo": "bar"})
+        sess.close()
+
+        is_true(called)
+
 
 class SessionStateTest(_fixtures.FixtureTest):
     run_inserts = None