sqlalchemy-orm

Model databases and query them with SQLAlchemy 2.0 in Python applications.

7|Updated Apr 9, 2024
One-click install
npx skills add https://github.com/figueroaignacio/ai --skill sqlalchemy-orm-figueroaignacio
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sqlalchemy-orm
Source: https://github.com/figueroaignacio/ai/tree/main/.agents/skills/sqlalchemy-orm
Command: npx skills add https://github.com/figueroaignacio/ai --skill sqlalchemy-orm-figueroaignacio

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

SQLAlchemy-ORM helps Python developers model complex databases, write expressive queries, and manage schema migrations with Alembic, reducing boilerplate and error-prone SQL.

Core Features & Use Cases

  • Declarative models with typed hints using Mapped and mapped_column for clear, type-safe schemas.
  • Relationships and joins (one-to-many and many-to-many) with back_populates and association tables.
  • Engine and session management, plus configuration for async and sync workflows.
  • Rich query patterns: selective filtering, ordering, joins, aggregations, and eager loading to avoid N+1 queries.
  • Migrations and evolution of schemas via Alembic, plus integration patterns for FastAPI, Flask, and Django.
  • Testing and tooling guidance for realistic data access layers.

Quick Start

Install SQLAlchemy, define your first Base and User model with mapped columns, create an engine and session, and run a simple select() query to verify setup.

Frequently Asked Questions about sqlalchemy-orm

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

FAQPage Schema
How do I define SQLAlchemy declarative models with type hints in Python?

SQLAlchemy declarative models use the Mapped and mapped_column constructs to define type-safe database schemas in Python. This approach provides clear type hinting for columns while reducing boilerplate code compared to writing raw SQL definitions.

How do I set up an async session with SQLAlchemy for FastAPI?

SQLAlchemy async session setup requires configuring an AsyncSession alongside an async engine for FastAPI integration. This enables asynchronous database interactions and efficient querying within modern Python web frameworks without blocking the event loop.

What's the best way to avoid N+1 queries when using SQLAlchemy ORM?

To avoid N+1 queries in SQLAlchemy ORM, apply eager loading strategies to your relationships. This fetches related objects in a single query or joined statement, preventing the performance bottleneck of issuing separate queries for each related item.

Does this Skill support Alembic migrations for FastAPI and Flask projects?

Yes, this Skill supports Alembic-based schema migrations for FastAPI, Flask, and Django projects. It provides tooling guidance and integration patterns to manage database schema evolution directly within these Python web frameworks.

How do I configure one-to-many and many-to-many relationships in SQLAlchemy?

SQLAlchemy ORM configures one-to-many and many-to-many relationships using back_populates and association tables. This mechanism explicitly links related models, ensuring bidirectional access and consistent state across joined database records.

Why should I use SQLAlchemy 2.0 for database modeling instead of raw SQL?

SQLAlchemy 2.0 models complex databases and enables expressive querying in Python, reducing error-prone boilerplate SQL. It provides modern type-hinted declarative models, relationship management, and session handling to streamline data access layers.