Skip to content

Issue In Async Session Setup #35

@Ahnaf19

Description

@Ahnaf19

Issue: AsyncSession setup with AsyncEngine causes type errors

The issue occurs in src/db/main.py.

What I was doing

  1. Following along your blog and yt video
  2. Used the latest libraries of all. This might be helpful:
# add it in a pyproject.toml and use uv to recreate the venv
dependencies = [
    "asyncpg>=0.30.0",
    "dotenv>=0.9.9",
    "fastapi[standard]>=0.116.1",
    "loguru>=0.7.3",
    "sqlmodel>=0.0.24",
]
  1. Initially managed to run the engine using the create_engine() function from sqlmodel. Engine was running fine, table was created properly.
  2. When got to the part defining asynchronous session then was facing the following issues:

Issues

 - In [src/db/main.py#L8](https://github.com/jod35/fastapi-beyond-CRUD/blob/main/src/db/main.py#L8)

the create_engine() from sqlmodel is not returning async engine. (Wrapping it in AsyncEngine(...) does not make it async - DB calls might still be blocked. Can block the event loop and cause performance issues in async applications.)
- In src/db/main.py#L16-L19
the sqlalchemy.orm.sessionmaker is sync-first and expects a sync Engine. But passing an AsyncEngine causes type-checker errors and can cause runtime issues. Takeaway: it was throwing red line errors like

Expected ":", No overloads for "init" match the provided arguments, Argument of type "AsyncEngine" cannot be assigned to parameter "bind" of type "_SessionBind | None" in function "init"
  Type "AsyncEngine" is not assignable to type "_SessionBind | None"
    "AsyncEngine" is not assignable to "Engine"
    "AsyncEngine" is not assignable to "Connection"
    "AsyncEngine" is not assignable to "None", Object of type "Session" cannot be used with "async with" because it does not correctly implement aenter
  Attribute "aenter" is unknown, Object of type "Session" cannot be used with "with" because it does not correctly implement aexit
  Attribute "aexit" is unknown, Return type of async generator function must be compatible with "AsyncGenerator[Unknown, Any]"
  "AsyncGenerator[Unknown, Unknown, Unknown]" is not assignable to "AsyncSession", Return type of async generator function must be compatible with "AsyncGenerator[Any, Any]"
  "AsyncGenerator[Any, Any, Any]" is not assignable to "AsyncSession"

Also rebuilding the session factory on every request → inefficient. Should be created once globally and reused.

My Workaround

So I was prompting in LLMs, finding no luck, I tried to recreate your exact package versions, still the same.

Then referred to this SQLAlchemy AsyncIO Doc and finally found the two things that solved the issue for me:

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
  • create_async_engine() returns async engine out of the box. Smoother.
  • Using async_sessionmaker was giving no trouble in making the async session. Noice.

Not sure if this gonna be compatible with the whole project; I haven't tried yet. So one way to find out, I think, is that I will complete this awesome course. Have my gratitude, by the way.

I will try to make a PR showing what changes I made in this script.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions