From: Mike Bayer Date: Sun, 27 Dec 2020 21:55:13 +0000 (-0500) Subject: Add support for AsyncSession.get() X-Git-Tag: rel_1_4_0b2~77 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=153671df9d4cd7f2cdb3e14e6221f529269885d9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add support for AsyncSession.get() Fixes: #5802 Change-Id: If59dbac4d4638563b7dcbc7bb2cb0df0a0144a2e --- diff --git a/doc/build/changelog/unreleased_14/5797.rst b/doc/build/changelog/unreleased_14/5797.rst index be322807af..0c1562fc93 100644 --- a/doc/build/changelog/unreleased_14/5797.rst +++ b/doc/build/changelog/unreleased_14/5797.rst @@ -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 diff --git a/lib/sqlalchemy/ext/asyncio/session.py b/lib/sqlalchemy/ext/asyncio/session.py index fc5b9cb448..bac2aa44b7 100644 --- a/lib/sqlalchemy/ext/asyncio/session.py +++ b/lib/sqlalchemy/ext/asyncio/session.py @@ -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, diff --git a/test/ext/asyncio/test_session_py3k.py b/test/ext/asyncio/test_session_py3k.py index c0ba8c2b3c..37e1b807b1 100644 --- a/test/ext/asyncio/test_session_py3k.py +++ b/test/ext/asyncio/test_session_py3k.py @@ -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):