r/FastAPI • • Aug 29 '24

Question Getting extra param *Args and **kwargs

Hii i made this api and i have given 3 param that is page, pagesize, search.
Why this two param are coming as required and why is it even coming?
how to solve this?

2 Upvotes

5 comments sorted by

View all comments

2

u/Adhesiveduck Aug 29 '24

Move the db_dependency into the app instance and use app.dependency_overrides:

import uvicorn
from fastapi import FastAPI, Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session


SQL_ALCHEMY_URL= "postgresql://postgres:root@localhost/users"


def get_session(session_maker: sessionmaker):
    def f():
        session = session_maker()
        try:
            yield session
        finally:
            session.close()

    return f


def create_app():
    app = FastAPI()   
    engine = create_engine(SQL_ALCHEMY_URL) 
    session_maker = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    app.dependency_overrides[Session] = get_session(session_maker)
    return app


app = create_app()

You can move the global SQL_ALCHEMY_URL, get_session wherever you want to keep it clean.