TypeError when using async function with FastAPI and SQLAlchemy session
I keep running into This might be a silly question, but I've searched everywhere and can't find a clear answer... I'm working with a `TypeError` when trying to use an asynchronous function with FastAPI and SQLAlchemy. My goal is to perform a database operation within an async route, but I'm running into issues when I attempt to commit the session. Here's the relevant portion of my code: ```python from fastapi import FastAPI, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base DATABASE_URL = 'postgresql+asyncpg://user:password@localhost/db' engine = create_async_engine(DATABASE_URL, echo=True) Base = declarative_base() async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession) app = FastAPI() @app.post("/items/") async def create_item(name: str, db: AsyncSession = Depends(async_session)): item = Item(name=name) # Assuming 'Item' is a SQLAlchemy model db.add(item) await db.commit() # This line raises the TypeError return item ``` I'm getting this behavior when I hit the endpoint: ``` TypeError: 'AsyncSession' object is not callable ``` Iโve checked the SQLAlchemy documentation and I know that I should be using `await` for asynchronous calls, but Iโm not sure what Iโm doing wrong here. I've also tried using `db.flush()` instead of `db.commit()`, but it doesnโt resolve the scenario. Can someone guide to understand what's going wrong with my session handling? I'm using FastAPI version 0.68.0 and SQLAlchemy version 1.4.23. I'd really appreciate any guidance on this. Has anyone else encountered this? I've been using Python for about a year now. For reference, this is a production mobile app. For reference, this is a production service. Am I missing something obvious?