‘AsyncSession’ object has no attribute ‘query’
Error in SQLAlchemy: ‘AsyncSession’ object has no attribute ‘query’.
What happened
SQLAlchemy updated the syntax from a certain version.
Instead of db.query(Task) now you need to use db.execute(select(Task))
Example
Previously worked:
async def fetch_all(db: Session, skip: int = 0, limit: int = 20):
return db.query(Task).order_by(Task.time.desc()).offset(skip).limit(limit).all()
Now it needs to work:
async def fetch_all(db: AsyncSession, skip: int = 0, limit: int = 20):
result = await db.execute(select(Task).order_by(Task.time.desc()).limit(20))
return result.scalars().all()