SQL (Relational) Databases

    The current page still doesn’t have a translation for this language.

    But you can help translating it: Contributing.

    FastAPI doesn’t require you to use a SQL (relational) database.

    But you can use any relational database that you want.

    Here we’ll see an example using .

    You can easily adapt it to any database supported by SQLAlchemy, like:

    • PostgreSQL
    • MySQL
    • SQLite
    • Oracle
    • Microsoft SQL Server, etc.

    In this example, we’ll use SQLite, because it uses a single file and Python has integrated support. So, you can copy this example and run it as is.

    Later, for your production application, you might want to use a database server like PostgreSQL.

    Tip

    There is an official project generator with FastAPI and PostgreSQL, all based on Docker, including a frontend and more tools: https://github.com/tiangolo/full-stack-fastapi-postgresql

    Note

    Notice that most of the code is the standard SQLAlchemy code you would use with any framework.

    The FastAPI specific code is as small as always.

    FastAPI works with any database and any style of library to talk to the database.

    A common pattern is to use an “ORM”: an “object-relational mapping” library.

    An ORM has tools to convert (“map“) between objects in code and database tables (“relations“).

    With an ORM, you normally create a class that represents a table in a SQL database, each attribute of the class represents a column, with a name and a type.

    For example a class Pet could represent a SQL table pets.

    And each instance object of that class represents a row in the database.

    For example an object orion_cat (an instance of Pet) could have an attribute orion_cat.type, for the column type. And the value of that attribute could be, e.g. "cat".

    These ORMs also have tools to make the connections or relations between tables or entities.

    This way, you could also have an attribute orion_cat.owner and the owner would contain the data for this pet’s owner, taken from the table owners.

    So, orion_cat.owner.name could be the name (from the name column in the owners table) of this pet’s owner.

    It could have a value like "Arquilian".

    And the ORM will do all the work to get the information from the corresponding table owners when you try to access it from your pet object.

    Common ORMs are for example: Django-ORM (part of the Django framework), SQLAlchemy ORM (part of SQLAlchemy, independent of framework) and Peewee (independent of framework), among others.

    Here we will see how to work with SQLAlchemy ORM.

    In a similar way you could use any other ORM.

    Tip

    There’s an equivalent article using Peewee here in the docs.

    File structure

    For these examples, let’s say you have a directory named my_super_project that contains a sub-directory called sql_app with a structure like this:

    The file __init__.py is just an empty file, but it tells Python that sql_app with all its modules (Python files) is a package.

    Now let’s see what each file/module does.

    Install SQLAlchemy

    First you need to install SQLAlchemy:

    Create the SQLAlchemy parts

    Let’s refer to the file sql_app/database.py.

    1. from sqlalchemy import create_engine
    2. from sqlalchemy.ext.declarative import declarative_base
    3. from sqlalchemy.orm import sessionmaker
    4. SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
    5. # SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
    6. engine = create_engine(
    7. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    8. )
    9. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    10. Base = declarative_base()

    Create a database URL for SQLAlchemy

    1. from sqlalchemy import create_engine
    2. from sqlalchemy.ext.declarative import declarative_base
    3. from sqlalchemy.orm import sessionmaker
    4. SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
    5. # SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
    6. engine = create_engine(
    7. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    8. )
    9. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    10. Base = declarative_base()

    In this example, we are “connecting” to a SQLite database (opening a file with the SQLite database).

    The file will be located at the same directory in the file sql_app.db.

    That’s why the last part is ./sql_app.db.

    If you were using a PostgreSQL database instead, you would just have to uncomment the line:

    1. SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"

    …and adapt it with your database data and credentials (equivalently for MySQL, MariaDB or any other).

    Tip

    This is the main line that you would have to modify if you wanted to use a different database.

    Create the SQLAlchemy engine

    The first step is to create a SQLAlchemy “engine”.

    We will later use this engine in other places.

    1. from sqlalchemy import create_engine
    2. from sqlalchemy.ext.declarative import declarative_base
    3. from sqlalchemy.orm import sessionmaker
    4. SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
    5. # SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
    6. engine = create_engine(
    7. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    8. )
    9. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    10. Base = declarative_base()

    Note

    The argument:

    1. connect_args={"check_same_thread": False}

    …is needed only for SQLite. It’s not needed for other databases.

    Technical Details

    By default SQLite will only allow one thread to communicate with it, assuming that each thread would handle an independent request.

    This is to prevent accidentally sharing the same connection for different things (for different requests).

    But in FastAPI, using normal functions (def) more than one thread could interact with the database for the same request, so we need to make SQLite know that it should allow that with connect_args={"check_same_thread": False}.

    Also, we will make sure each request gets its own database connection session in a dependency, so there’s no need for that default mechanism.

    Create a SessionLocal class

    Each instance of the SessionLocal class will be a database session. The class itself is not a database session yet.

    But once we create an instance of the SessionLocal class, this instance will be the actual database session.

    We name it SessionLocal to distinguish it from the Session we are importing from SQLAlchemy.

    We will use Session (the one imported from SQLAlchemy) later.

    To create the SessionLocal class, use the function sessionmaker:

    1. from sqlalchemy import create_engine
    2. from sqlalchemy.ext.declarative import declarative_base
    3. from sqlalchemy.orm import sessionmaker
    4. SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
    5. # SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
    6. engine = create_engine(
    7. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    8. )
    9. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    10. Base = declarative_base()

    Create a Base class

    Now we will use the function declarative_base() that returns a class.

    Later we will inherit from this class to create each of the database models or classes (the ORM models):

    1. from sqlalchemy import create_engine
    2. from sqlalchemy.ext.declarative import declarative_base
    3. from sqlalchemy.orm import sessionmaker
    4. SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
    5. # SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
    6. engine = create_engine(
    7. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    8. )
    9. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    10. Base = declarative_base()

    Let’s now see the file sql_app/models.py.

    Create SQLAlchemy models from the Base class

    We will use this Base class we created before to create the SQLAlchemy models.

    Tip

    SQLAlchemy uses the term “model“ to refer to these classes and instances that interact with the database.

    But Pydantic also uses the term “model“ to refer to something different, the data validation, conversion, and documentation classes and instances.

    Import Base from database (the file database.py from above).

    Create classes that inherit from it.

    These classes are the SQLAlchemy models.

    1. from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
    2. from sqlalchemy.orm import relationship
    3. from .database import Base
    4. class User(Base):
    5. __tablename__ = "users"
    6. id = Column(Integer, primary_key=True, index=True)
    7. email = Column(String, unique=True, index=True)
    8. hashed_password = Column(String)
    9. is_active = Column(Boolean, default=True)
    10. items = relationship("Item", back_populates="owner")
    11. class Item(Base):
    12. __tablename__ = "items"
    13. id = Column(Integer, primary_key=True, index=True)
    14. title = Column(String, index=True)
    15. description = Column(String, index=True)
    16. owner_id = Column(Integer, ForeignKey("users.id"))
    17. owner = relationship("User", back_populates="items")

    The __tablename__ attribute tells SQLAlchemy the name of the table to use in the database for each of these models.

    Now create all the model (class) attributes.

    Each of these attributes represents a column in its corresponding database table.

    We use Column from SQLAlchemy as the default value.

    And we pass a SQLAlchemy class “type”, as Integer, String, and Boolean, that defines the type in the database, as an argument.

    1. from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
    2. from sqlalchemy.orm import relationship
    3. from .database import Base
    4. class User(Base):
    5. __tablename__ = "users"
    6. id = Column(Integer, primary_key=True, index=True)
    7. email = Column(String, unique=True, index=True)
    8. hashed_password = Column(String)
    9. is_active = Column(Boolean, default=True)
    10. items = relationship("Item", back_populates="owner")
    11. class Item(Base):
    12. __tablename__ = "items"
    13. id = Column(Integer, primary_key=True, index=True)
    14. title = Column(String, index=True)
    15. description = Column(String, index=True)
    16. owner_id = Column(Integer, ForeignKey("users.id"))
    17. owner = relationship("User", back_populates="items")

    Create the relationships

    Now create the relationships.

    For this, we use relationship provided by SQLAlchemy ORM.

    This will become, more or less, a “magic” attribute that will contain the values from other tables related to this one.

    1. from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
    2. from sqlalchemy.orm import relationship
    3. from .database import Base
    4. class User(Base):
    5. __tablename__ = "users"
    6. id = Column(Integer, primary_key=True, index=True)
    7. email = Column(String, unique=True, index=True)
    8. hashed_password = Column(String)
    9. is_active = Column(Boolean, default=True)
    10. items = relationship("Item", back_populates="owner")
    11. class Item(Base):
    12. __tablename__ = "items"
    13. id = Column(Integer, primary_key=True, index=True)
    14. title = Column(String, index=True)
    15. description = Column(String, index=True)
    16. owner_id = Column(Integer, ForeignKey("users.id"))
    17. owner = relationship("User", back_populates="items")

    When you access my_user.items, SQLAlchemy will actually go and fetch the items from the database in the items table and populate them here.

    And when accessing the attribute owner in an Item, it will contain a User SQLAlchemy model from the users table. It will use the owner_id attribute/column with its foreign key to know which record to get from the users table.

    Create the Pydantic models

    Now let’s check the file sql_app/schemas.py.

    Tip

    To avoid confusion between the SQLAlchemy models and the Pydantic models, we will have the file models.py with the SQLAlchemy models, and the file schemas.py with the Pydantic models.

    These Pydantic models define more or less a “schema” (a valid data shape).

    So this will help us avoiding confusion while using both.

    Create initial Pydantic models / schemas

    Create an ItemBase and UserBase Pydantic models (or let’s say “schemas”) to have common attributes while creating or reading data.

    And create an ItemCreate and UserCreate that inherit from them (so they will have the same attributes), plus any additional data (attributes) needed for creation.

    So, the user will also have a password when creating it.

    But for security, the password won’t be in other Pydantic models, for example, it won’t be sent from the API when reading a user.

    Python 3.9 and abovePython 3.10 and above

    1. from typing import List, Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: List[Item] = []
    21. class Config:
    22. orm_mode = True
    1. from typing import Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: list[Item] = []
    21. class Config:
    22. orm_mode = True
    1. from pydantic import BaseModel
    2. class ItemBase(BaseModel):
    3. title: str
    4. description: str | None = None
    5. class ItemCreate(ItemBase):
    6. pass
    7. class Item(ItemBase):
    8. id: int
    9. owner_id: int
    10. class Config:
    11. orm_mode = True
    12. class UserBase(BaseModel):
    13. email: str
    14. class UserCreate(UserBase):
    15. password: str
    16. class User(UserBase):
    17. id: int
    18. is_active: bool
    19. items: list[Item] = []
    20. class Config:
    21. orm_mode = True

    SQLAlchemy style and Pydantic style

    Notice that SQLAlchemy models define attributes using =, and pass the type as a parameter to Column, like in:

    1. name = Column(String)

    while Pydantic models declare the types using :, the new type annotation syntax/type hints:

    1. name: str

    Have it in mind, so you don’t get confused when using = and : with them.

    Create Pydantic models / schemas for reading / returning

    Now create Pydantic models (schemas) that will be used when reading data, when returning it from the API.

    For example, before creating an item, we don’t know what will be the ID assigned to it, but when reading it (when returning it from the API) we will already know its ID.

    The same way, when reading a user, we can now declare that items will contain the items that belong to this user.

    Not only the IDs of those items, but all the data that we defined in the Pydantic model for reading items: Item.

    Python 3.6 and abovePython 3.9 and abovePython 3.10 and above

    1. from typing import Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: list[Item] = []
    21. class Config:
    22. orm_mode = True
    1. from pydantic import BaseModel
    2. class ItemBase(BaseModel):
    3. title: str
    4. description: str | None = None
    5. class ItemCreate(ItemBase):
    6. pass
    7. class Item(ItemBase):
    8. id: int
    9. owner_id: int
    10. class Config:
    11. orm_mode = True
    12. class UserBase(BaseModel):
    13. email: str
    14. class UserCreate(UserBase):
    15. password: str
    16. class User(UserBase):
    17. id: int
    18. is_active: bool
    19. items: list[Item] = []
    20. class Config:
    21. orm_mode = True

    Tip

    Notice that the User, the Pydantic model that will be used when reading a user (returning it from the API) doesn’t include the password.

    Use Pydantic’s orm_mode

    Now, in the Pydantic models for reading, Item and User, add an internal Config class.

    This Config class is used to provide configurations to Pydantic.

    In the Config class, set the attribute orm_mode = True.

    Python 3.6 and abovePython 3.9 and abovePython 3.10 and above

    1. from typing import List, Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: List[Item] = []
    21. class Config:
    22. orm_mode = True
    1. from typing import Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: list[Item] = []
    21. class Config:
    22. orm_mode = True
    1. class ItemBase(BaseModel):
    2. title: str
    3. description: str | None = None
    4. class ItemCreate(ItemBase):
    5. pass
    6. class Item(ItemBase):
    7. owner_id: int
    8. class Config:
    9. orm_mode = True
    10. class UserBase(BaseModel):
    11. email: str
    12. class UserCreate(UserBase):
    13. password: str
    14. class User(UserBase):
    15. id: int
    16. is_active: bool
    17. items: list[Item] = []
    18. class Config:
    19. orm_mode = True

    Tip

    Notice it’s assigning a value with =, like:

    orm_mode = True

    It doesn’t use : as for the type declarations before.

    This is setting a config value, not declaring a type.

    Pydantic’s orm_mode will tell the Pydantic model to read the data even if it is not a dict, but an ORM model (or any other arbitrary object with attributes).

    This way, instead of only trying to get the id value from a dict, as in:

    1. id = data["id"]

    it will also try to get it from an attribute, as in:

    1. id = data.id

    And with this, the Pydantic model is compatible with ORMs, and you can just declare it in the response_model argument in your path operations.

    You will be able to return a database model and it will read the data from it.

    Technical Details about ORM mode

    SQLAlchemy and many others are by default “lazy loading”.

    That means, for example, that they don’t fetch the data for relationships from the database unless you try to access the attribute that would contain that data.

    For example, accessing the attribute items:

    1. current_user.items

    would make SQLAlchemy go to the items table and get the items for this user, but not before.

    Without orm_mode, if you returned a SQLAlchemy model from your path operation, it wouldn’t include the relationship data.

    Even if you declared those relationships in your Pydantic models.

    But with ORM mode, as Pydantic itself will try to access the data it needs from attributes (instead of assuming a dict), you can declare the specific data you want to return and it will be able to go and get it, even from ORMs.

    CRUD utils

    Now let’s see the file sql_app/crud.py.

    In this file we will have reusable functions to interact with the data in the database.

    CRUD comes from: Create, Read, Update, and Delete.

    …although in this example we are only creating and reading.

    Read data

    Import Session from sqlalchemy.orm, this will allow you to declare the type of the db parameters and have better type checks and completion in your functions.

    Import models (the SQLAlchemy models) and schemas (the Pydantic models / schemas).

    Create utility functions to:

    • Read a single user by ID and by email.
    • Read multiple users.
    • Read multiple items.
    1. from sqlalchemy.orm import Session
    2. from . import models, schemas
    3. def get_user(db: Session, user_id: int):
    4. return db.query(models.User).filter(models.User.id == user_id).first()
    5. def get_user_by_email(db: Session, email: str):
    6. return db.query(models.User).filter(models.User.email == email).first()
    7. def get_users(db: Session, skip: int = 0, limit: int = 100):
    8. return db.query(models.User).offset(skip).limit(limit).all()
    9. def create_user(db: Session, user: schemas.UserCreate):
    10. fake_hashed_password = user.password + "notreallyhashed"
    11. db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
    12. db.add(db_user)
    13. db.commit()
    14. db.refresh(db_user)
    15. return db_user
    16. def get_items(db: Session, skip: int = 0, limit: int = 100):
    17. return db.query(models.Item).offset(skip).limit(limit).all()
    18. def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
    19. db_item = models.Item(**item.dict(), owner_id=user_id)
    20. db.add(db_item)
    21. db.commit()
    22. db.refresh(db_item)
    23. return db_item

    Tip

    By creating functions that are only dedicated to interacting with the database (get a user or an item) independent of your path operation function, you can more easily reuse them in multiple parts and also add unit tests for them.

    Now create utility functions to create data.

    The steps are:

    • Create a SQLAlchemy model instance with your data.
    • add that instance object to your database session.
    • commit the changes to the database (so that they are saved).
    • refresh your instance (so that it contains any new data from the database, like the generated ID).
    1. from sqlalchemy.orm import Session
    2. from . import models, schemas
    3. def get_user(db: Session, user_id: int):
    4. return db.query(models.User).filter(models.User.id == user_id).first()
    5. def get_user_by_email(db: Session, email: str):
    6. return db.query(models.User).filter(models.User.email == email).first()
    7. def get_users(db: Session, skip: int = 0, limit: int = 100):
    8. return db.query(models.User).offset(skip).limit(limit).all()
    9. def create_user(db: Session, user: schemas.UserCreate):
    10. fake_hashed_password = user.password + "notreallyhashed"
    11. db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
    12. db.add(db_user)
    13. db.commit()
    14. db.refresh(db_user)
    15. return db_user
    16. def get_items(db: Session, skip: int = 0, limit: int = 100):
    17. return db.query(models.Item).offset(skip).limit(limit).all()
    18. def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
    19. db_item = models.Item(**item.dict(), owner_id=user_id)
    20. db.add(db_item)
    21. db.commit()
    22. db.refresh(db_item)
    23. return db_item

    Tip

    The SQLAlchemy model for User contains a hashed_password that should contain a secure hashed version of the password.

    But as what the API client provides is the original password, you need to extract it and generate the hashed password in your application.

    And then pass the hashed_password argument with the value to save.

    Warning

    This example is not secure, the password is not hashed.

    In a real life application you would need to hash the password and never save them in plaintext.

    For more details, go back to the Security section in the tutorial.

    Here we are focusing only on the tools and mechanics of databases.

    Tip

    Instead of passing each of the keyword arguments to Item and reading each one of them from the Pydantic model, we are generating a dict with the Pydantic model‘s data with:

    item.dict()

    and then we are passing the dict‘s key-value pairs as the keyword arguments to the SQLAlchemy Item, with:

    Item(**item.dict())

    And then we pass the extra keyword argument owner_id that is not provided by the Pydantic model, with:

    Item(**item.dict(), owner_id=user_id)

    Main FastAPI app

    And now in the file sql_app/main.py let’s integrate and use all the other parts we created before.

    Create the database tables

    In a very simplistic way create the database tables:

    Python 3.6 and abovePython 3.9 and above

    1. from typing import List
    2. from fastapi import Depends, FastAPI, HTTPException
    3. from sqlalchemy.orm import Session
    4. from . import crud, models, schemas
    5. from .database import SessionLocal, engine
    6. models.Base.metadata.create_all(bind=engine)
    7. app = FastAPI()
    8. # Dependency
    9. def get_db():
    10. db = SessionLocal()
    11. try:
    12. yield db
    13. finally:
    14. db.close()
    15. @app.post("/users/", response_model=schemas.User)
    16. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    17. db_user = crud.get_user_by_email(db, email=user.email)
    18. if db_user:
    19. raise HTTPException(status_code=400, detail="Email already registered")
    20. return crud.create_user(db=db, user=user)
    21. @app.get("/users/", response_model=List[schemas.User])
    22. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    23. users = crud.get_users(db, skip=skip, limit=limit)
    24. return users
    25. @app.get("/users/{user_id}", response_model=schemas.User)
    26. def read_user(user_id: int, db: Session = Depends(get_db)):
    27. db_user = crud.get_user(db, user_id=user_id)
    28. if db_user is None:
    29. raise HTTPException(status_code=404, detail="User not found")
    30. return db_user
    31. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    32. def create_item_for_user(
    33. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    34. ):
    35. return crud.create_user_item(db=db, item=item, user_id=user_id)
    36. @app.get("/items/", response_model=List[schemas.Item])
    37. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    38. items = crud.get_items(db, skip=skip, limit=limit)
    39. return items
    1. from fastapi import Depends, FastAPI, HTTPException
    2. from sqlalchemy.orm import Session
    3. from . import crud, models, schemas
    4. from .database import SessionLocal, engine
    5. models.Base.metadata.create_all(bind=engine)
    6. app = FastAPI()
    7. # Dependency
    8. def get_db():
    9. db = SessionLocal()
    10. try:
    11. yield db
    12. finally:
    13. db.close()
    14. @app.post("/users/", response_model=schemas.User)
    15. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    16. db_user = crud.get_user_by_email(db, email=user.email)
    17. if db_user:
    18. raise HTTPException(status_code=400, detail="Email already registered")
    19. return crud.create_user(db=db, user=user)
    20. @app.get("/users/", response_model=list[schemas.User])
    21. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    22. users = crud.get_users(db, skip=skip, limit=limit)
    23. return users
    24. @app.get("/users/{user_id}", response_model=schemas.User)
    25. def read_user(user_id: int, db: Session = Depends(get_db)):
    26. db_user = crud.get_user(db, user_id=user_id)
    27. if db_user is None:
    28. raise HTTPException(status_code=404, detail="User not found")
    29. return db_user
    30. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    31. def create_item_for_user(
    32. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    33. ):
    34. return crud.create_user_item(db=db, item=item, user_id=user_id)
    35. @app.get("/items/", response_model=list[schemas.Item])
    36. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    37. items = crud.get_items(db, skip=skip, limit=limit)
    38. return items

    Alembic Note

    Normally you would probably initialize your database (create tables, etc) with Alembic.

    And you would also use Alembic for “migrations” (that’s its main job).

    You can find an example of Alembic in a FastAPI project in the templates from . Specifically in the alembic directory in the source code.

    Create a dependency

    Now use the SessionLocal class we created in the sql_app/database.py file to create a dependency.

    We need to have an independent database session/connection (SessionLocal) per request, use the same session through all the request and then close it after the request is finished.

    And then a new session will be created for the next request.

    For that, we will create a new dependency with yield, as explained before in the section about Dependencies with yield.

    Our dependency will create a new SQLAlchemy SessionLocal that will be used in a single request, and then close it once the request is finished.

    Python 3.6 and abovePython 3.9 and above

    1. from typing import List
    2. from fastapi import Depends, FastAPI, HTTPException
    3. from sqlalchemy.orm import Session
    4. from . import crud, models, schemas
    5. from .database import SessionLocal, engine
    6. models.Base.metadata.create_all(bind=engine)
    7. app = FastAPI()
    8. # Dependency
    9. def get_db():
    10. db = SessionLocal()
    11. try:
    12. yield db
    13. finally:
    14. db.close()
    15. @app.post("/users/", response_model=schemas.User)
    16. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    17. db_user = crud.get_user_by_email(db, email=user.email)
    18. if db_user:
    19. raise HTTPException(status_code=400, detail="Email already registered")
    20. return crud.create_user(db=db, user=user)
    21. @app.get("/users/", response_model=List[schemas.User])
    22. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    23. users = crud.get_users(db, skip=skip, limit=limit)
    24. return users
    25. @app.get("/users/{user_id}", response_model=schemas.User)
    26. def read_user(user_id: int, db: Session = Depends(get_db)):
    27. db_user = crud.get_user(db, user_id=user_id)
    28. if db_user is None:
    29. raise HTTPException(status_code=404, detail="User not found")
    30. return db_user
    31. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    32. def create_item_for_user(
    33. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    34. ):
    35. return crud.create_user_item(db=db, item=item, user_id=user_id)
    36. @app.get("/items/", response_model=List[schemas.Item])
    37. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    38. items = crud.get_items(db, skip=skip, limit=limit)
    39. return items
    1. from fastapi import Depends, FastAPI, HTTPException
    2. from sqlalchemy.orm import Session
    3. from . import crud, models, schemas
    4. from .database import SessionLocal, engine
    5. models.Base.metadata.create_all(bind=engine)
    6. app = FastAPI()
    7. # Dependency
    8. def get_db():
    9. db = SessionLocal()
    10. try:
    11. yield db
    12. finally:
    13. db.close()
    14. @app.post("/users/", response_model=schemas.User)
    15. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    16. db_user = crud.get_user_by_email(db, email=user.email)
    17. if db_user:
    18. raise HTTPException(status_code=400, detail="Email already registered")
    19. return crud.create_user(db=db, user=user)
    20. @app.get("/users/", response_model=list[schemas.User])
    21. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    22. users = crud.get_users(db, skip=skip, limit=limit)
    23. return users
    24. @app.get("/users/{user_id}", response_model=schemas.User)
    25. def read_user(user_id: int, db: Session = Depends(get_db)):
    26. db_user = crud.get_user(db, user_id=user_id)
    27. if db_user is None:
    28. raise HTTPException(status_code=404, detail="User not found")
    29. return db_user
    30. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    31. def create_item_for_user(
    32. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    33. ):
    34. return crud.create_user_item(db=db, item=item, user_id=user_id)
    35. @app.get("/items/", response_model=list[schemas.Item])
    36. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    37. items = crud.get_items(db, skip=skip, limit=limit)
    38. return items

    Info

    We put the creation of the SessionLocal() and handling of the requests in a try block.

    And then we close it in the finally block.

    This way we make sure the database session is always closed after the request. Even if there was an exception while processing the request.

    But you can’t raise another exception from the exit code (after yield). See more in

    And then, when using the dependency in a path operation function, we declare it with the type Session we imported directly from SQLAlchemy.

    This will then give us better editor support inside the path operation function, because the editor will know that the db parameter is of type Session:

    Python 3.6 and abovePython 3.9 and above

    1. from typing import List
    2. from fastapi import Depends, FastAPI, HTTPException
    3. from sqlalchemy.orm import Session
    4. from . import crud, models, schemas
    5. from .database import SessionLocal, engine
    6. models.Base.metadata.create_all(bind=engine)
    7. app = FastAPI()
    8. # Dependency
    9. def get_db():
    10. db = SessionLocal()
    11. try:
    12. yield db
    13. finally:
    14. db.close()
    15. @app.post("/users/", response_model=schemas.User)
    16. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    17. db_user = crud.get_user_by_email(db, email=user.email)
    18. if db_user:
    19. raise HTTPException(status_code=400, detail="Email already registered")
    20. return crud.create_user(db=db, user=user)
    21. @app.get("/users/", response_model=List[schemas.User])
    22. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    23. users = crud.get_users(db, skip=skip, limit=limit)
    24. return users
    25. @app.get("/users/{user_id}", response_model=schemas.User)
    26. def read_user(user_id: int, db: Session = Depends(get_db)):
    27. db_user = crud.get_user(db, user_id=user_id)
    28. if db_user is None:
    29. raise HTTPException(status_code=404, detail="User not found")
    30. return db_user
    31. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    32. def create_item_for_user(
    33. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    34. ):
    35. return crud.create_user_item(db=db, item=item, user_id=user_id)
    36. @app.get("/items/", response_model=List[schemas.Item])
    37. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    38. items = crud.get_items(db, skip=skip, limit=limit)
    39. return items

    Technical Details

    The parameter db is actually of type SessionLocal, but this class (created with sessionmaker()) is a “proxy” of a SQLAlchemy Session, so, the editor doesn’t really know what methods are provided.

    But by declaring the type as Session, the editor now can know the available methods (.add(), .query(), .commit(), etc) and can provide better support (like completion). The type declaration doesn’t affect the actual object.

    Create your FastAPI path operations

    Now, finally, here’s the standard FastAPI path operations code.

    Python 3.6 and abovePython 3.9 and above

    1. from typing import List
    2. from fastapi import Depends, FastAPI, HTTPException
    3. from sqlalchemy.orm import Session
    4. from . import crud, models, schemas
    5. from .database import SessionLocal, engine
    6. models.Base.metadata.create_all(bind=engine)
    7. app = FastAPI()
    8. # Dependency
    9. def get_db():
    10. db = SessionLocal()
    11. try:
    12. yield db
    13. finally:
    14. db.close()
    15. @app.post("/users/", response_model=schemas.User)
    16. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    17. db_user = crud.get_user_by_email(db, email=user.email)
    18. if db_user:
    19. raise HTTPException(status_code=400, detail="Email already registered")
    20. return crud.create_user(db=db, user=user)
    21. @app.get("/users/", response_model=List[schemas.User])
    22. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    23. users = crud.get_users(db, skip=skip, limit=limit)
    24. return users
    25. @app.get("/users/{user_id}", response_model=schemas.User)
    26. def read_user(user_id: int, db: Session = Depends(get_db)):
    27. db_user = crud.get_user(db, user_id=user_id)
    28. if db_user is None:
    29. raise HTTPException(status_code=404, detail="User not found")
    30. return db_user
    31. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    32. def create_item_for_user(
    33. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    34. ):
    35. return crud.create_user_item(db=db, item=item, user_id=user_id)
    36. @app.get("/items/", response_model=List[schemas.Item])
    37. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    38. items = crud.get_items(db, skip=skip, limit=limit)
    39. return items
    1. from fastapi import Depends, FastAPI, HTTPException
    2. from sqlalchemy.orm import Session
    3. from . import crud, models, schemas
    4. from .database import SessionLocal, engine
    5. models.Base.metadata.create_all(bind=engine)
    6. app = FastAPI()
    7. # Dependency
    8. def get_db():
    9. db = SessionLocal()
    10. try:
    11. yield db
    12. finally:
    13. db.close()
    14. @app.post("/users/", response_model=schemas.User)
    15. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    16. db_user = crud.get_user_by_email(db, email=user.email)
    17. if db_user:
    18. raise HTTPException(status_code=400, detail="Email already registered")
    19. return crud.create_user(db=db, user=user)
    20. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    21. users = crud.get_users(db, skip=skip, limit=limit)
    22. return users
    23. @app.get("/users/{user_id}", response_model=schemas.User)
    24. def read_user(user_id: int, db: Session = Depends(get_db)):
    25. db_user = crud.get_user(db, user_id=user_id)
    26. if db_user is None:
    27. raise HTTPException(status_code=404, detail="User not found")
    28. return db_user
    29. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    30. def create_item_for_user(
    31. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    32. ):
    33. return crud.create_user_item(db=db, item=item, user_id=user_id)
    34. @app.get("/items/", response_model=list[schemas.Item])
    35. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    36. items = crud.get_items(db, skip=skip, limit=limit)
    37. return items

    We are creating the database session before each request in the dependency with yield, and then closing it afterwards.

    And then we can create the required dependency in the path operation function, to get that session directly.

    With that, we can just call crud.get_user directly from inside of the path operation function and use that session.

    Tip

    Notice that the values you return are SQLAlchemy models, or lists of SQLAlchemy models.

    But as all the path operations have a response_model with Pydantic models / schemas using orm_mode, the data declared in your Pydantic models will be extracted from them and returned to the client, with all the normal filtering and validation.

    Tip

    Also notice that there are response_models that have standard Python types like List[schemas.Item].

    But as the content/parameter of that List is a Pydantic model with orm_mode, the data will be retrieved and returned to the client as normally, without problems.

    About def vs async def

    Here we are using SQLAlchemy code inside of the path operation function and in the dependency, and, in turn, it will go and communicate with an external database.

    That could potentially require some “waiting”.

    But as SQLAlchemy doesn’t have compatibility for using await directly, as would be with something like:

    1. user = await db.query(User).first()

    …and instead we are using:

    1. user = db.query(User).first()

    Then we should declare the path operation functions and the dependency without async def, just with a normal def, as:

    1. @app.get("/users/{user_id}", response_model=schemas.User)
    2. def read_user(user_id: int, db: Session = Depends(get_db)):
    3. db_user = crud.get_user(db, user_id=user_id)
    4. ...

    Info

    If you need to connect to your relational database asynchronously, see Async SQL (Relational) Databases.

    Very Technical Details

    If you are curious and have a deep technical knowledge, you can check the very technical details of how this async def vs def is handled in the docs.

    Because we are using SQLAlchemy directly and we don’t require any kind of plug-in for it to work with FastAPI, we could integrate database migrations with directly.

    And as the code related to SQLAlchemy and the SQLAlchemy models lives in separate independent files, you would even be able to perform the migrations with Alembic without having to install FastAPI, Pydantic, or anything else.

    The same way, you would be able to use the same SQLAlchemy models and utilities in other parts of your code that are not related to FastAPI.

    For example, in a background task worker with Celery, , or ARQ.

    Review all the files

    Remember you should have a directory named my_super_project that contains a sub-directory called sql_app.

    sql_app should have the following files:

    • sql_app/__init__.py: is an empty file.

    • sql_app/database.py:

    1. from sqlalchemy import create_engine
    2. from sqlalchemy.ext.declarative import declarative_base
    3. from sqlalchemy.orm import sessionmaker
    4. SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
    5. # SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
    6. engine = create_engine(
    7. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
    8. )
    9. SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    10. Base = declarative_base()
    • sql_app/models.py:
    1. from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
    2. from sqlalchemy.orm import relationship
    3. from .database import Base
    4. class User(Base):
    5. __tablename__ = "users"
    6. id = Column(Integer, primary_key=True, index=True)
    7. email = Column(String, unique=True, index=True)
    8. hashed_password = Column(String)
    9. is_active = Column(Boolean, default=True)
    10. items = relationship("Item", back_populates="owner")
    11. class Item(Base):
    12. __tablename__ = "items"
    13. id = Column(Integer, primary_key=True, index=True)
    14. title = Column(String, index=True)
    15. description = Column(String, index=True)
    16. owner_id = Column(Integer, ForeignKey("users.id"))
    17. owner = relationship("User", back_populates="items")
    • sql_app/schemas.py:

    Python 3.6 and abovePython 3.9 and abovePython 3.10 and above

    1. from typing import List, Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: List[Item] = []
    21. class Config:
    22. orm_mode = True
    1. from typing import Union
    2. from pydantic import BaseModel
    3. class ItemBase(BaseModel):
    4. title: str
    5. description: Union[str, None] = None
    6. class ItemCreate(ItemBase):
    7. pass
    8. class Item(ItemBase):
    9. id: int
    10. owner_id: int
    11. class Config:
    12. orm_mode = True
    13. class UserBase(BaseModel):
    14. email: str
    15. class UserCreate(UserBase):
    16. password: str
    17. class User(UserBase):
    18. id: int
    19. is_active: bool
    20. items: list[Item] = []
    21. class Config:
    22. orm_mode = True
    1. from pydantic import BaseModel
    2. class ItemBase(BaseModel):
    3. title: str
    4. description: str | None = None
    5. class ItemCreate(ItemBase):
    6. pass
    7. class Item(ItemBase):
    8. id: int
    9. owner_id: int
    10. class Config:
    11. orm_mode = True
    12. class UserBase(BaseModel):
    13. email: str
    14. class UserCreate(UserBase):
    15. password: str
    16. class User(UserBase):
    17. id: int
    18. is_active: bool
    19. items: list[Item] = []
    20. class Config:
    21. orm_mode = True
    • sql_app/crud.py:
    1. from sqlalchemy.orm import Session
    2. from . import models, schemas
    3. def get_user(db: Session, user_id: int):
    4. return db.query(models.User).filter(models.User.id == user_id).first()
    5. def get_user_by_email(db: Session, email: str):
    6. return db.query(models.User).filter(models.User.email == email).first()
    7. def get_users(db: Session, skip: int = 0, limit: int = 100):
    8. return db.query(models.User).offset(skip).limit(limit).all()
    9. def create_user(db: Session, user: schemas.UserCreate):
    10. fake_hashed_password = user.password + "notreallyhashed"
    11. db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
    12. db.add(db_user)
    13. db.commit()
    14. db.refresh(db_user)
    15. return db_user
    16. def get_items(db: Session, skip: int = 0, limit: int = 100):
    17. return db.query(models.Item).offset(skip).limit(limit).all()
    18. def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
    19. db_item = models.Item(**item.dict(), owner_id=user_id)
    20. db.add(db_item)
    21. db.commit()
    22. db.refresh(db_item)
    23. return db_item
    • sql_app/main.py:

    Python 3.6 and abovePython 3.9 and above

    1. from typing import List
    2. from fastapi import Depends, FastAPI, HTTPException
    3. from sqlalchemy.orm import Session
    4. from . import crud, models, schemas
    5. from .database import SessionLocal, engine
    6. models.Base.metadata.create_all(bind=engine)
    7. app = FastAPI()
    8. # Dependency
    9. def get_db():
    10. db = SessionLocal()
    11. try:
    12. yield db
    13. finally:
    14. db.close()
    15. @app.post("/users/", response_model=schemas.User)
    16. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    17. db_user = crud.get_user_by_email(db, email=user.email)
    18. if db_user:
    19. raise HTTPException(status_code=400, detail="Email already registered")
    20. return crud.create_user(db=db, user=user)
    21. @app.get("/users/", response_model=List[schemas.User])
    22. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    23. users = crud.get_users(db, skip=skip, limit=limit)
    24. return users
    25. @app.get("/users/{user_id}", response_model=schemas.User)
    26. def read_user(user_id: int, db: Session = Depends(get_db)):
    27. db_user = crud.get_user(db, user_id=user_id)
    28. if db_user is None:
    29. raise HTTPException(status_code=404, detail="User not found")
    30. return db_user
    31. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    32. def create_item_for_user(
    33. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    34. ):
    35. return crud.create_user_item(db=db, item=item, user_id=user_id)
    36. @app.get("/items/", response_model=List[schemas.Item])
    37. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    38. items = crud.get_items(db, skip=skip, limit=limit)
    39. return items
    1. from fastapi import Depends, FastAPI, HTTPException
    2. from sqlalchemy.orm import Session
    3. from . import crud, models, schemas
    4. from .database import SessionLocal, engine
    5. models.Base.metadata.create_all(bind=engine)
    6. app = FastAPI()
    7. # Dependency
    8. def get_db():
    9. db = SessionLocal()
    10. try:
    11. yield db
    12. finally:
    13. db.close()
    14. @app.post("/users/", response_model=schemas.User)
    15. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    16. db_user = crud.get_user_by_email(db, email=user.email)
    17. if db_user:
    18. raise HTTPException(status_code=400, detail="Email already registered")
    19. return crud.create_user(db=db, user=user)
    20. @app.get("/users/", response_model=list[schemas.User])
    21. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    22. users = crud.get_users(db, skip=skip, limit=limit)
    23. return users
    24. @app.get("/users/{user_id}", response_model=schemas.User)
    25. def read_user(user_id: int, db: Session = Depends(get_db)):
    26. db_user = crud.get_user(db, user_id=user_id)
    27. if db_user is None:
    28. raise HTTPException(status_code=404, detail="User not found")
    29. return db_user
    30. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    31. def create_item_for_user(
    32. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    33. ):
    34. return crud.create_user_item(db=db, item=item, user_id=user_id)
    35. @app.get("/items/", response_model=list[schemas.Item])
    36. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    37. items = crud.get_items(db, skip=skip, limit=limit)
    38. return items

    Check it

    You can copy this code and use it as is.

    Info

    In fact, the code shown here is part of the tests. As most of the code in these docs.

    Then you can run it with Uvicorn:

    SQL (Relational/) Databases - 图2

    And then, you can open your browser at .

    And you will be able to interact with your FastAPI application, reading data from a real database:

    Interact with the database directly

    If you want to explore the SQLite database (file) directly, independently of FastAPI, to debug its contents, add tables, columns, records, modify data, etc. you can use .

    It will look like this:

    SQL (Relational/) Databases - 图4

    You can also use an online SQLite browser like SQLite Viewer or .

    If you can’t use dependencies with yield — for example, if you are not using Python 3.7 and can’t install the “backports” mentioned above for Python 3.6 — you can set up the session in a “middleware” in a similar way.

    A “middleware” is basically a function that is always executed for each request, with some code executed before, and some code executed after the endpoint function.

    Create a middleware

    The middleware we’ll add (just a function) will create a new SQLAlchemy SessionLocal for each request, add it to the request and then close it once the request is finished.

    Python 3.6 and abovePython 3.9 and above

    1. from typing import List
    2. from fastapi import Depends, FastAPI, HTTPException, Request, Response
    3. from sqlalchemy.orm import Session
    4. from . import crud, models, schemas
    5. from .database import SessionLocal, engine
    6. models.Base.metadata.create_all(bind=engine)
    7. app = FastAPI()
    8. @app.middleware("http")
    9. async def db_session_middleware(request: Request, call_next):
    10. response = Response("Internal server error", status_code=500)
    11. try:
    12. request.state.db = SessionLocal()
    13. response = await call_next(request)
    14. finally:
    15. request.state.db.close()
    16. return response
    17. # Dependency
    18. def get_db(request: Request):
    19. return request.state.db
    20. @app.post("/users/", response_model=schemas.User)
    21. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    22. db_user = crud.get_user_by_email(db, email=user.email)
    23. if db_user:
    24. raise HTTPException(status_code=400, detail="Email already registered")
    25. return crud.create_user(db=db, user=user)
    26. @app.get("/users/", response_model=List[schemas.User])
    27. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    28. users = crud.get_users(db, skip=skip, limit=limit)
    29. return users
    30. @app.get("/users/{user_id}", response_model=schemas.User)
    31. def read_user(user_id: int, db: Session = Depends(get_db)):
    32. db_user = crud.get_user(db, user_id=user_id)
    33. if db_user is None:
    34. raise HTTPException(status_code=404, detail="User not found")
    35. return db_user
    36. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    37. def create_item_for_user(
    38. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    39. ):
    40. return crud.create_user_item(db=db, item=item, user_id=user_id)
    41. @app.get("/items/", response_model=List[schemas.Item])
    42. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    43. items = crud.get_items(db, skip=skip, limit=limit)
    44. return items
    1. from fastapi import Depends, FastAPI, HTTPException, Request, Response
    2. from sqlalchemy.orm import Session
    3. from . import crud, models, schemas
    4. from .database import SessionLocal, engine
    5. models.Base.metadata.create_all(bind=engine)
    6. app = FastAPI()
    7. @app.middleware("http")
    8. async def db_session_middleware(request: Request, call_next):
    9. response = Response("Internal server error", status_code=500)
    10. try:
    11. request.state.db = SessionLocal()
    12. response = await call_next(request)
    13. finally:
    14. request.state.db.close()
    15. return response
    16. # Dependency
    17. def get_db(request: Request):
    18. return request.state.db
    19. @app.post("/users/", response_model=schemas.User)
    20. def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
    21. db_user = crud.get_user_by_email(db, email=user.email)
    22. if db_user:
    23. raise HTTPException(status_code=400, detail="Email already registered")
    24. return crud.create_user(db=db, user=user)
    25. @app.get("/users/", response_model=list[schemas.User])
    26. def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    27. users = crud.get_users(db, skip=skip, limit=limit)
    28. return users
    29. @app.get("/users/{user_id}", response_model=schemas.User)
    30. def read_user(user_id: int, db: Session = Depends(get_db)):
    31. db_user = crud.get_user(db, user_id=user_id)
    32. if db_user is None:
    33. raise HTTPException(status_code=404, detail="User not found")
    34. return db_user
    35. @app.post("/users/{user_id}/items/", response_model=schemas.Item)
    36. def create_item_for_user(
    37. user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
    38. ):
    39. return crud.create_user_item(db=db, item=item, user_id=user_id)
    40. @app.get("/items/", response_model=list[schemas.Item])
    41. def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    42. items = crud.get_items(db, skip=skip, limit=limit)
    43. return items

    Info

    We put the creation of the SessionLocal() and handling of the requests in a try block.

    And then we close it in the finally block.

    This way we make sure the database session is always closed after the request. Even if there was an exception while processing the request.

    request.state is a property of each Request object. It is there to store arbitrary objects attached to the request itself, like the database session in this case. You can read more about it in .

    For us in this case, it helps us ensure a single database session is used through all the request, and then closed afterwards (in the middleware).

    Dependencies with yield or middleware

    Adding a middleware here is similar to what a dependency with yield does, with some differences:

    • It requires more code and is a bit more complex.
    • The middleware has to be an async function.
      • If there is code in it that has to “wait” for the network, it could “block” your application there and degrade performance a bit.
      • Although it’s probably not very problematic here with the way SQLAlchemy works.
      • But if you added more code to the middleware that had a lot of I/O waiting, it could then be problematic.
    • A middleware is run for every request.
      • So, a connection will be created for every request.

    Tip

    It’s probably better to use dependencies with when they are enough for the use case.

    Info

    Dependencies with yield were added recently to FastAPI.