Skip to content

engine singleton Built in SQLmodel or make my own ? #1510

Discussion options

You must be logged in to vote

If you use this style of dependency for getting the database session - https://sqlmodel.tiangolo.com/tutorial/fastapi/session-with-dependency/#create-a-fastapi-dependency

..then you only need to just create the engine in the same module as where you define the get_session() function. e.g. if you have a "db.py", you can just have something like:

# ... imports, etc ...

engine = create_engine(sqlite_url, echo=False)

def get_session():
    with Session(engine) as session:
        yield session

Then when you define your router endpoints, just import and use get_session with Depends as described in the documentation I linked to above.

That's roughly a simplified version of how I do it, anyway...

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #123 on August 13, 2025 08:42.