]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Apply minor update to async_orm example
authorGord Thompson <gord@gordthompson.com>
Wed, 20 Oct 2021 21:15:37 +0000 (15:15 -0600)
committerGord Thompson <gord@gordthompson.com>
Wed, 20 Oct 2021 21:15:37 +0000 (15:15 -0600)
- Fixed import to avoid MovedIn20Warning.

- Separated drop_all() and create_all() into separate
context managers to avoid dropping and creating the
same table within the same transaction. Apparently
some databases (e.g., CockroachDB) don't allow that.

Change-Id: Id26d7d719871a75ffb78c6af589658666802fb2f

examples/asyncio/async_orm.py

index d3355791c36b622e83cc4caef41796a5aaefddd3..174ebf30b5f4f11f795946b3ebb824f771f15898 100644 (file)
@@ -13,8 +13,8 @@ from sqlalchemy import Integer
 from sqlalchemy import String
 from sqlalchemy.ext.asyncio import AsyncSession
 from sqlalchemy.ext.asyncio import create_async_engine
-from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.future import select
+from sqlalchemy.orm import declarative_base
 from sqlalchemy.orm import relationship
 from sqlalchemy.orm import selectinload
 from sqlalchemy.orm import sessionmaker
@@ -53,6 +53,7 @@ async def async_main():
 
     async with engine.begin() as conn:
         await conn.run_sync(Base.metadata.drop_all)
+    async with engine.begin() as conn:
         await conn.run_sync(Base.metadata.create_all)
 
     # expire_on_commit=False will prevent attributes from being expired