TypeError when using SQLAlchemy with asyncpg in Python 3.9 for PostgreSQL
I'm writing unit tests and I've been banging my head against this for hours. I'm currently working on an asynchronous application using FastAPI and SQLAlchemy to interface with PostgreSQL via asyncpg. While trying to execute a simple query, I'm running into a `TypeError` that seems to stem from how I'm configuring my session. I'm using Python 3.9 with SQLAlchemy 1.4.0 and asyncpg 0.23.0. Hereβs the code that causes the scenario: ```python from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker, declarative_base Base = declarative_base() engine = create_async_engine('postgresql+asyncpg://user:password@localhost/dbname', echo=True) session_local = sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False) async def get_user(user_id): async with session_local() as session: result = await session.execute(f"SELECT * FROM users WHERE id={user_id}") return result.scalar_one() ``` When I try to call `get_user(1)`, I get the following behavior: ``` TypeError: 'coroutine' object is not subscriptable ``` I suspect it has to do with how I'm using the `session.execute()` method. Iβve also tried to change the SQL statement to use parameters instead, but the result is the same. Can someone explain why this is happening and how to fix it? Any best practices for using SQLAlchemy with asyncpg in this context would also be appreciated. For context: I'm using Python on CentOS. What's the best practice here? I'm developing on Windows 11 with Python. Am I approaching this the right way?