SQLAlchemy

Implement async SQLAlchemy 2.x data access patterns for FastAPI projects.

Updated May 7, 2026
One-click install
npx skills add https://github.com/lgzarturo/codeconductor --skill sqlalchemy-lgzarturo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: SQLAlchemy
Source: https://github.com/lgzarturo/codeconductor/tree/main/.agents/skills/sqlalchemy
Command: npx skills add https://github.com/lgzarturo/codeconductor --skill sqlalchemy-lgzarturo

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers implement robust async SQLAlchemy data access patterns for FastAPI projects, covering models, sessions, queries, migrations, and transactional workflows to ensure scalable and safe database interactions.

Core Features & Use Cases

  • Engine and session setup with AsyncSession and engine configuration, including expire_on_commit handling.
  • Base model structure with a TimestampMixin to standardize timestamps across entities.
  • Declarative models and relationships using SQLAlchemy 2.x style with proper lazy loading safeguards.
  • Async queries, bulk operations, and Alembic-based migrations to evolve schema safely in FastAPI projects.
  • Guidance on common pitfalls (N+1 queries, proper eager loading, and transaction boundaries).

Quick Start

Define an async engine, an AsyncSession factory, and a sample model with Alembic migrations to start using SQLAlchemy 2.x in FastAPI.

Frequently Asked Questions about SQLAlchemy

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up async SQLAlchemy sessions in a FastAPI project?

To set up async SQLAlchemy sessions in FastAPI, define an async engine and an AsyncSession factory, ensuring proper expire_on_commit handling for safe database interactions.

What is the best way to structure SQLAlchemy 2.x models with FastAPI?

The best way to structure SQLAlchemy 2.x models is using a declarative Base with a TimestampMixin, standardizing timestamps and defining relationships with proper lazy loading safeguards.

How do I avoid N+1 queries with async SQLAlchemy in FastAPI?

Avoid N+1 queries in async SQLAlchemy by applying proper eager loading strategies and respecting transaction boundaries to ensure scalable and safe database access patterns.

Can I use Alembic migrations with async SQLAlchemy in FastAPI workflows?

Yes, you can integrate Alembic-based migrations with async SQLAlchemy in FastAPI workflows to safely evolve your database schema alongside your declarative models.

How do I handle transactions and safe commit patterns in FastAPI async sessions?

Handle transactions in FastAPI async sessions by using safe commit and flush patterns designed for SQLAlchemy 2.x, preventing data inconsistencies within your transactional workflows.

Does SQLAlchemy 2.x async require special handling for relationships in FastAPI?

Yes, SQLAlchemy 2.x async in FastAPI requires special handling for relationships to prevent lazy loading errors, necessitating explicit eager loading and careful session management.