FastAPI is designed to be fast to develop, easy to understand, and highly performant. It uses Python type hints to validate, serialize, and deserialize data, which makes it very intuitive for developers. FastAPI also supports asynchronous programming out - of - the - box, which can significantly improve the performance of your API.
SQLAlchemy provides a high - level, Pythonic way to interact with databases. It has two main components: the Core, which is a SQL abstraction layer, and the ORM, which maps Python classes to database tables. The ORM allows you to work with database records as Python objects, making it easier to write database - related code.
Integrating FastAPI with SQLAlchemy means using SQLAlchemy to handle all database operations within a FastAPI application. This includes creating, reading, updating, and deleting records in the database. The integration allows you to build APIs that can interact with databases in a seamless and efficient manner.
First, create a new Python virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
Then, install the necessary packages:
pip install fastapi sqlalchemy uvicorn
You also need to install a database driver. For example, if you are using SQLite, you can use the built - in sqlite3
driver. For PostgreSQL, you can install psycopg2
:
pip install psycopg2-binary
Here is an example of defining a simple database model using SQLAlchemy:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
name = Column(String)
email = Column(String, unique=True, index=True)
In this example, we define a User
class that represents a table named users
in the database. The id
column is the primary key, and the email
column has a unique constraint.
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# For SQLite
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
# For PostgreSQL
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} if "sqlite" in SQLALCHEMY_DATABASE_URL else {}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
The create_engine
function creates a database engine, which is used to connect to the database. The SessionLocal
class is used to create database sessions. The get_db
function is a dependency that can be used in FastAPI routes to get a database session.
from fastapi import Depends, FastAPI
from sqlalchemy.orm import Session
app = FastAPI()
@app.post("/users/")
def create_user(name: str, email: str, db: Session = Depends(get_db)):
new_user = User(name=name, email=email)
db.add(new_user)
db.commit()
db.refresh(new_user)
return new_user
@app.get("/users/{user_id}")
def read_user(user_id: int, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
return user
@app.put("/users/{user_id}")
def update_user(user_id: int, name: str, email: str, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if user:
user.name = name
user.email = email
db.commit()
db.refresh(user)
return user
@app.delete("/users/{user_id}")
def delete_user(user_id: int, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if user:
db.delete(user)
db.commit()
return {"message": "User deleted successfully"}
get_db
function, use dependency injection to manage database sessions in FastAPI routes. This ensures that the database session is properly opened and closed for each request.asyncpg
for PostgreSQL.pytest
to test your FastAPI application with SQLAlchemy.Integrating FastAPI with SQLAlchemy is a powerful combination for building database - driven APIs. FastAPI provides a fast and easy - to - use framework for building APIs, while SQLAlchemy offers a flexible and Pythonic way to interact with databases. By following the concepts, practices, and examples in this blog post, you can build robust and efficient APIs that can handle all types of database operations.