Skip to content

Passing Database Session to Depends Function #11805

Answered by binbjz
crosfan asked this question in Questions
Discussion options

You must be logged in to vote

Problem Analysis

The core issue is how to pass the database session in FastAPI dependencies so that it can be used in other endpoints. For example, in the get_current_user function, the db session needs to be passed.

Solution

We need to modify the get_current_user and other related functions so that they can receive the database session as a dependency. The specific steps are as follows:

  1. Define a dependency to get the database session:
    Ensure you have a dependency function to get the database session, such as get_session.

  2. Modify the get_current_user function:
    Add the database session as a parameter and use FastAPI's Depends dependency injection mechanism to get the session.

  3. Modify o…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@crosfan
Comment options

Answer selected by crosfan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants