summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Caselli <cfederico87@gmail.com>2021-12-11 08:09:20 +0000
committerGerrit Code Review <gerrit@ci3.zzzcomputing.com>2021-12-11 08:09:20 +0000
commit6528a5fbd10f70487219f328dcec27d58c56b837 (patch)
treeeb56699ac37f3fcd2b0d26a42d959496ae85404b
parent33fd0e29a7332baa367f51d536ceaca2988f0c62 (diff)
parenta7c004dff86a0bdd228f70800f8f8e3872d4aad1 (diff)
downloadsqlalchemy-6528a5fbd10f70487219f328dcec27d58c56b837.tar.gz
Merge "Add execution options to ``Session.get``" into main
-rw-r--r--doc/build/changelog/unreleased_14/7410.rst6
-rw-r--r--lib/sqlalchemy/orm/session.py15
-rw-r--r--test/orm/test_scoping.py1
-rw-r--r--test/orm/test_session.py18
4 files changed, 40 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_14/7410.rst b/doc/build/changelog/unreleased_14/7410.rst
new file mode 100644
index 000000000..7b4e8efbf
--- /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 f75f2ac9f..e921bb8f0 100644
--- a/lib/sqlalchemy/orm/session.py
+++ b/lib/sqlalchemy/orm/session.py
@@ -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(
diff --git a/test/orm/test_scoping.py b/test/orm/test_scoping.py
index c5458f5ce..9b255bf5e 100644
--- a/test/orm/test_scoping.py
+++ b/test/orm/test_scoping.py
@@ -157,6 +157,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 1b92da248..e821a7c20 100644
--- a/test/orm/test_session.py
+++ b/test/orm/test_session.py
@@ -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