1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Having trouble with asyc_sessiomaker in FastAPI

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm buiding endpoints with FastAPI, PostgreSQL as database, and the driver is asyncpg associated with SQLAlchemy for asynchronous. As mentioned in the title, I'm having trouble with async_sessionmaker, it keeps showing: 'async_sessionmaker' object does not support the asynchronous context manager protocol.

    Here the part of code in repository:


    class GenreRepositoryImpl(GenreRepository):

    def __init__(self, sessionmaker: async_sessionmaker[AsyncSession]):
    self._sessionmaker = sessionmaker

    async def create(self, genre: Genre) -> Genre:
    genre_entity = GenreEntityMappers.from_domain(genre)

    async with self._sessionmaker() as session:
    session.add(genre_entity)
    await session.commit()
    await session.refresh(genre_entity)

    return GenreEntityMappers.to_domain(genre_entity)


    Somehow it works when I use it as transaction with begin(), I don't understand what's wrong.

    Continue reading...

Compartilhe esta Página