Skip to content

Conversation

Jaroslav2001
Copy link

@Jaroslav2001 Jaroslav2001 commented Sep 4, 2022

Added new AsyncSession() and create_async_engine()

Example using sqlmodel in asynchronous mode.

import asyncio
from typing import Optional

from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select


class Hero(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    secret_name: str
    age: Optional[int] = None


sqlite_file_name = "database.db"
sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"

engine = create_async_engine(sqlite_url, echo=True)


async def create_db_and_tables():
    async with engine.begin() as session:
        await session.run_sync(SQLModel.metadata.create_all)


async def create_heroes():
    hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
    hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
    hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)

    session = AsyncSession(engine)
    session.add(hero_1)
    session.add(hero_2)
    session.add(hero_3)

    await session.commit()

    await session.close()


async def select_heroes():
    async with AsyncSession(engine) as session:
        statement = select(Hero)
        results = await session.exec(statement)
        for hero in results:
            print(hero)


async def main():
    await create_db_and_tables()
    await create_heroes()
    await select_heroes()


if __name__ == "__main__":
    asyncio.run(main())

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2022

📝 Docs preview for commit 491980b at: https://6315333e4f16cf4a290a1268--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2022

📝 Docs preview for commit 6d459f7 at: https://63153c13e31c9630787851f4--sqlmodel.netlify.app

@codecov
Copy link

codecov bot commented Sep 5, 2022

Codecov Report

Base: 98.49% // Head: 97.75% // Decreases project coverage by -0.73% ⚠️

Coverage data is based on head (80e0179) compared to base (ea79c47).
Patch coverage: 86.42% of modified lines in pull request are covered.

❗ Current head 80e0179 differs from pull request most recent head 5b2ad95. Consider uploading reports for the commit 5b2ad95 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #435      +/-   ##
==========================================
- Coverage   98.49%   97.75%   -0.74%     
==========================================
  Files         185      190       +5     
  Lines        5856     6472     +616     
==========================================
+ Hits         5768     6327     +559     
- Misses         88      145      +57     
Impacted Files Coverage Δ
sqlmodel/engine/create.py 63.88% <56.45%> (-5.63%) ⬇️
sqlmodel/ext/asyncio/session.py 88.00% <82.35%> (+88.00%) ⬆️
docs_src/advanced/asyncio/tutorial001.py 100.00% <100.00%> (ø)
docs_src/advanced/asyncio/tutorial001_py36.py 100.00% <100.00%> (ø)
...sts/test_advanced/test_asyncio/test_tutorial001.py 100.00% <100.00%> (ø)
sqlmodel/main.py 84.92% <0.00%> (ø)
tests/test_main.py 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2022

📝 Docs preview for commit 5028d19 at: https://63154b3f4f16cf58450a1d74--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2022

📝 Docs preview for commit 80e0179 at: https://631554b448c9b8611409c2f9--sqlmodel.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 5b2ad95 at: https://639ce0e4a12b8e030c9de2d1--sqlmodel.netlify.app

@tiangolo tiangolo added the feature New feature or request label Oct 22, 2023
@hakan-77
Copy link

This is a great feature, any progress?

@tiangolo

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 5, 2025
Copy link
Contributor

github-actions bot commented Sep 5, 2025

This pull request has a merge conflict that needs to be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicts Automatically generated when a PR has a merge conflict feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants