ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Mastering Database Integration with SQLAlchemy in FastAPI

author
Generated by
Shahrukh Quraishi

15/10/2024

fastapi

Sign in to read full article

Introduction to SQLAlchemy in FastAPI

When building robust web applications with FastAPI, integrating a database is often a crucial requirement. SQLAlchemy, a powerful and flexible Object-Relational Mapping (ORM) tool, provides an excellent solution for database integration in Python. In this blog post, we'll explore how to use SQLAlchemy with FastAPI to create efficient and scalable database-driven applications.

Setting Up SQLAlchemy with FastAPI

To get started, you'll need to install the necessary dependencies:

pip install fastapi sqlalchemy databases

Next, let's set up the database connection and create a base model:

from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker DATABASE_URL = "sqlite:///./test.db" engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()

Defining Models

With the database connection set up, we can now define our models. Let's create a simple User model:

from sqlalchemy import Column, Integer, String class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) username = Column(String, unique=True, index=True) email = Column(String, unique=True, index=True) hashed_password = Column(String)

Creating Database Tables

To create the database tables based on our models, we need to call Base.metadata.create_all(engine) when starting our application:

def create_tables(): Base.metadata.create_all(bind=engine) if __name__ == "__main__": create_tables() uvicorn.run(app, host="0.0.0.0", port=8000)

Dependency Injection for Database Sessions

To ensure proper handling of database sessions, we'll create a dependency that yields a database session:

from fastapi import Depends from sqlalchemy.orm import Session def get_db(): db = SessionLocal() try: yield db finally: db.close()

CRUD Operations

Now that we have our database set up, let's implement some CRUD (Create, Read, Update, Delete) operations:

Create

from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from . import models, schemas router = APIRouter() @router.post("/users/", response_model=schemas.User) def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): db_user = models.User(username=user.username, email=user.email, hashed_password=user.password) db.add(db_user) db.commit() db.refresh(db_user) return db_user

Read

@router.get("/users/{user_id}", response_model=schemas.User) def read_user(user_id: int, db: Session = Depends(get_db)): db_user = db.query(models.User).filter(models.User.id == user_id).first() if db_user is None: raise HTTPException(status_code=404, detail="User not found") return db_user

Update

@router.put("/users/{user_id}", response_model=schemas.User) def update_user(user_id: int, user: schemas.UserUpdate, db: Session = Depends(get_db)): db_user = db.query(models.User).filter(models.User.id == user_id).first() if db_user is None: raise HTTPException(status_code=404, detail="User not found") for key, value in user.dict().items(): setattr(db_user, key, value) db.commit() db.refresh(db_user) return db_user

Delete

@router.delete("/users/{user_id}", response_model=schemas.User) def delete_user(user_id: int, db: Session = Depends(get_db)): db_user = db.query(models.User).filter(models.User.id == user_id).first() if db_user is None: raise HTTPException(status_code=404, detail="User not found") db.delete(db_user) db.commit() return db_user

Handling Relationships

SQLAlchemy makes it easy to define and work with relationships between models. Let's add a Post model and establish a one-to-many relationship with the User model:

from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.orm import relationship class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) username = Column(String, unique=True, index=True) email = Column(String, unique=True, index=True) hashed_password = Column(String) posts = relationship("Post", back_populates="author") class Post(Base): __tablename__ = "posts" id = Column(Integer, primary_key=True, index=True) title = Column(String, index=True) content = Column(String) author_id = Column(Integer, ForeignKey("users.id")) author = relationship("User", back_populates="posts")

Now you can easily create posts for users and retrieve all posts for a specific user:

@router.post("/users/{user_id}/posts/", response_model=schemas.Post) def create_post_for_user(user_id: int, post: schemas.PostCreate, db: Session = Depends(get_db)): db_user = db.query(models.User).filter(models.User.id == user_id).first() if db_user is None: raise HTTPException(status_code=404, detail="User not found") db_post = models.Post(**post.dict(), author_id=user_id) db.add(db_post) db.commit() db.refresh(db_post) return db_post @router.get("/users/{user_id}/posts/", response_model=List[schemas.Post]) def read_user_posts(user_id: int, db: Session = Depends(get_db)): db_user = db.query(models.User).filter(models.User.id == user_id).first() if db_user is None: raise HTTPException(status_code=404, detail="User not found") return db_user.posts

Advanced Querying

SQLAlchemy provides powerful querying capabilities. Here are some examples of more advanced queries:

Filtering and Ordering

@router.get("/users/", response_model=List[schemas.User]) def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return db.query(models.User).order_by(models.User.username).offset(skip).limit(limit).all()

Joining Tables

@router.get("/users-with-posts/", response_model=List[schemas.UserWithPosts]) def read_users_with_posts(db: Session = Depends(get_db)): return db.query(models.User).options(joinedload(models.User.posts)).all()

Aggregations

from sqlalchemy import func @router.get("/user-post-counts/", response_model=List[schemas.UserPostCount]) def read_user_post_counts(db: Session = Depends(get_db)): return db.query(models.User.id, models.User.username, func.count(models.Post.id).label("post_count")).\ join(models.Post).\ group_by(models.User.id).\ all()

Conclusion

By integrating SQLAlchemy with FastAPI, you can create powerful, database-driven applications with ease. This combination allows for efficient data management, complex querying, and seamless integration with your FastAPI routes. As you continue to explore SQLAlchemy, you'll discover even more advanced features that can help you build robust and scalable web applications.

Popular tags

fastapisqlalchemydatabase

Share now!

Like & bookmark

Related collections

  • Python with Redis Cache

    08/11/2024 · Python

  • Python Basics: Comprehensive Guide

    21/09/2024 · Python

  • Mastering Computer Vision with OpenCV

    06/12/2024 · Python

  • Seaborn: Data Visualization from Basics to Advanced

    06/10/2024 · Python

  • Python with MongoDB: A Practical Guide

    08/11/2024 · Python

Related articles

  • Advanced Pattern Design and Best Practices in LangChain

    26/10/2024 · Python

  • Mastering Imbalanced Data Handling in Python with Scikit-learn

    15/11/2024 · Python

  • Mastering Variables in LangGraph

    17/11/2024 · Python

  • Unlocking Question Answering with Transformers in Python

    14/11/2024 · Python

  • Unleashing the Power of Transformers for NLP Tasks with Python and Hugging Face

    14/11/2024 · Python

  • Debugging and Visualizing PyTorch Models

    14/11/2024 · Python

  • Python Fundamentals for Web Development

    26/10/2024 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design