docs: Add documentation for async SQLModel usage by shuteng8787-sudo · Pull Request #1757 · fastapi/sqlmodel

Add Documentation for Async SQLModel Usage

Description

This PR adds comprehensive documentation for using SQLModel with async and await, resolving Issue #626.

Although SQLModel has had AsyncSession support for some time, it was not documented in the official tutorial or user guide. This PR addresses that by adding a new tutorial section dedicated to asynchronous usage.

Changes

Documentation

  • Created docs/tutorial/async/index.md: A new tutorial page covering:
    • Asynchronous engine configuration with create_async_engine.
    • Using aiosqlite for asynchronous SQLite support.
    • Basic usage of AsyncSession and the exec() method.
    • Integration with FastAPI using async dependencies.
    • Common pitfalls and best practices (e.g., lazy loading, eager loading with selectinload).
  • Updated docs/advanced/index.md to link to the new async tutorial.
  • Updated mkdocs.yml to include the new tutorial in the navigation under the Advanced User Guide.

Source Code Examples

  • Created docs_src/tutorial/async/:
    • tutorial001_py310.py: Modern Python 3.10+ async example.
    • tutorial001_py39.py: Python 3.9 async example.
    • tutorial001.py: Baseline async example.

Library Improvements

  • Updated sqlmodel/ext/asyncio/__init__.py to export AsyncSession, allowing for cleaner imports:
    from sqlmodel.ext.asyncio import AsyncSession

Related Issues

Fixes #626