mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-05-16 13:47:20 -04:00
c3f102c9fe
It's better, the majority of these changes look more readable to me. also found some docstrings that had formatting / quoting issues. Change-Id: I582a45fde3a5648b2f36bab96bad56881321899b
91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
"""Illustrates use of the sqlalchemy.ext.asyncio.AsyncSession object
|
|
for asynchronous ORM use.
|
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
from sqlalchemy import Column
|
|
from sqlalchemy import ForeignKey
|
|
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 relationship
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
class A(Base):
|
|
__tablename__ = "a"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
data = Column(String)
|
|
bs = relationship("B")
|
|
|
|
|
|
class B(Base):
|
|
__tablename__ = "b"
|
|
id = Column(Integer, primary_key=True)
|
|
a_id = Column(ForeignKey("a.id"))
|
|
data = Column(String)
|
|
|
|
|
|
async def async_main():
|
|
"""Main program function."""
|
|
|
|
engine = create_async_engine(
|
|
"postgresql+asyncpg://scott:tiger@localhost/test",
|
|
echo=True,
|
|
)
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
async with AsyncSession(engine) as session:
|
|
async with session.begin():
|
|
session.add_all(
|
|
[
|
|
A(bs=[B(), B()], data="a1"),
|
|
A(bs=[B()], data="a2"),
|
|
A(bs=[B(), B()], data="a3"),
|
|
]
|
|
)
|
|
|
|
# for relationship loading, eager loading should be applied.
|
|
stmt = select(A).options(selectinload(A.bs))
|
|
|
|
# AsyncSession.execute() is used for 2.0 style ORM execution
|
|
# (same as the synchronous API).
|
|
result = await session.execute(stmt)
|
|
|
|
# result is a buffered Result object.
|
|
for a1 in result.scalars():
|
|
print(a1)
|
|
for b1 in a1.bs:
|
|
print(b1)
|
|
|
|
# for streaming ORM results, AsyncSession.stream() may be used.
|
|
result = await session.stream(stmt)
|
|
|
|
# result is a streaming AsyncResult object.
|
|
async for a1 in result.scalars():
|
|
print(a1)
|
|
for b1 in a1.bs:
|
|
print(b1)
|
|
|
|
result = await session.execute(select(A).order_by(A.id))
|
|
|
|
a1 = result.scalars().first()
|
|
|
|
a1.data = "new data"
|
|
|
|
await session.commit()
|
|
|
|
|
|
asyncio.run(async_main())
|