docs: Add AsyncSession and async tools tutorial (#626) by alexbthundiyil-spec · Pull Request #1831 · fastapi/sqlmodel
The tutorial is a useful addition, but there are a few technical issues worth addressing. First, the variable name sqlite_url is misleading when it holds a PostgreSQL connection string ("postgresql+asyncpg://..."). This will confuse readers following along — should be renamed to something like database_url or async_url. Second, the create_heroes function calls session.commit() and session.refresh() but has no error handling — an async tutorial should ideally demonstrate try/except with rollback, or at minimum mention that exceptions during commit leave the session in a broken state and need await session.rollback().
Third, the FastAPI integration example references engine and Hero without imports or definitions, which means it won't run as-is. Since this is a tutorial, each code block should either be self-contained or clearly reference the earlier block. Finally, the get_session dependency yields a session but doesn't show what happens on exception — FastAPI's dependency injection will call session.close() via the generator cleanup, but session.rollback() on error is a common pattern that's worth showing for write operations. The read-only example is fine as a starting point, but a note about write endpoints needing explicit rollback handling would round this out.