]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add support for AsyncSession.get()
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 27 Dec 2020 21:55:13 +0000 (16:55 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 27 Dec 2020 21:55:13 +0000 (16:55 -0500)
Fixes: #5802
Change-Id: If59dbac4d4638563b7dcbc7bb2cb0df0a0144a2e

doc/build/changelog/unreleased_14/5797.rst
lib/sqlalchemy/ext/asyncio/session.py
test/ext/asyncio/test_session_py3k.py

index be322807afd6f51259f715fa750ec689d98a4a4f..0c1562fc937cfa81b39274518ed47f226398d8c9 100644 (file)
@@ -1,8 +1,9 @@
 .. change::
     :tags: usecase, orm, asyncio
-    :tickets: 5796, 5797
+    :tickets: 5796, 5797, 5802
 
-    Added :meth:`_asyncio.AsyncSession.scalar` as well as support for
+    Added :meth:`_asyncio.AsyncSession.scalar`,
+    :meth:`_asyncio.AsyncSession.get` as well as support for
     :meth:`_orm.sessionmaker.begin` to work as an async context manager with
     :class:`_asyncio.AsyncSession`.  Also added
     :meth:`_asyncio.AsyncSession.in_transaction` accessor.
\ No newline at end of file
index fc5b9cb4485239e438b4f9bf435596abbf771211..bac2aa44b760054c3d71dbf64c554ff62928676b 100644 (file)
@@ -165,6 +165,30 @@ class AsyncSession:
         )
         return result.scalar()
 
+    async def get(
+        self,
+        entity,
+        ident,
+        options=None,
+        populate_existing=False,
+        with_for_update=None,
+        identity_token=None,
+    ):
+        """Return an instance based on the given primary key identifier,
+        or ``None`` if not found.
+
+
+        """
+        return await greenlet_spawn(
+            self.sync_session.get,
+            entity,
+            ident,
+            options=options,
+            populate_existing=populate_existing,
+            with_for_update=with_for_update,
+            identity_token=identity_token,
+        )
+
     async def stream(
         self,
         statement,
index c0ba8c2b3c04ca2921800b653e305ac5edf740c9..37e1b807b11fa9cce34696c74ba64c086c8ca1e7 100644 (file)
@@ -64,6 +64,21 @@ class AsyncSessionQueryTest(AsyncFixture):
         result = await async_session.scalar(stmt)
         eq_(result, 7)
 
+    @async_test
+    async def test_get(self, async_session):
+        User = self.classes.User
+
+        u1 = await async_session.get(User, 7)
+
+        eq_(u1.name, "jack")
+
+        u2 = await async_session.get(User, 7)
+
+        is_(u1, u2)
+
+        u3 = await async_session.get(User, 12)
+        is_(u3, None)
+
     @async_test
     @testing.requires.independent_cursors
     async def test_stream_partitions(self, async_session):