0

I'm new in fast api. I'm trying to get user from my table but getting this error:

user = await db.query(User).filter(User.login == data['login']).first()

AttributeError: 'Depends' object has no attribute 'query'

here is my server/database.py

import os
from sqlmodel import SQLModel, Session
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker


DATABASE_URL = os.environ.get("DATABASE_URL")


engine = create_async_engine(DATABASE_URL, echo = True, future = True)



async def init_db():
    async with engine.begin() as conn:
        # await conn.run_sync(SQLModel.metadata.drop_all)
        await conn.run_sync(SQLModel.metadata.create_all) # init is ok, db table working
        pass


async def get_session() -> AsyncSession:
    async_session = sessionmaker(
        engine, class_ = AsyncSession, 
        expire_on_commit = False
    )
    async with async_session() as session:
        yield session

here is server/web_server.py

from fastapi import Depends, FastAPI
from sqlalchemy.future import select
from fastapi_socketio import SocketManager
from fastapi_login import LoginManager
import json
from database import get_session, init_db
from models import User, File


app = FastAPI()
sio = SocketManager(app = app)

@app.on_event("startup")
async def on_startup():
    await init_db() # is ok



@app.sio.on('connect')
async def handle_connect(sid, connection_data):
    await app.sio.emit('connect', 'User joined') # is ok


@app.sio.on('authorize')
async def authorize(sid, message, db = Depends(get_session)):
    data = json.loads(message) # is ok
    user = await db.query(User).filter(User.login == data['login']).first() # error in this line
    print(user)



if __name__ == '__main__':
    import uvicorn
    uvicorn.run("web_server:app", host = '0.0.0.0', port = 8000, reload = True, debug = False)

I'm using socketio for my app, socketio.on('authorize') message contains data = {'login' : '...', 'password' : '...'} and I want to use this to find user in my postgresql table to check password and authorize it. So having problems with 'get_session', query, execute and commit not working with it, getting the same error AttributeError: 'Depends' object has no attribute ...

Charwisd
  • 53
  • 9

0 Answers0