sqlalchemy-orm

Implements SQLAlchemy 2.0 ORM models, queries, migrations, and async database sessions in Python.

Updated Sep 14, 2026
One-click install
npx skills add https://github.com/desarrolloainia/nuevo_circuito_mir --skill sqlalchemy-orm-desarrolloainia
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: sqlalchemy-orm
Source: https://github.com/desarrolloainia/nuevo_circuito_mir/tree/main/backend/.agents/skills/sqlalchemy
Command: npx skills add https://github.com/desarrolloainia/nuevo_circuito_mir --skill sqlalchemy-orm-desarrolloainia

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires sqlalchemy, alembic.

What problem does it solve? Building database-backed Python applications requires writing correct ORM models, efficient queries, schema migrations, and session management, which is error-prone without established patterns. ## Core Features & Use Cases - Declarative Models with Type Hints: Define tables using SQLAlchemy 2.0 Mapped types, mapped_column, and relationship mappings for one-to-many and many-to-many associations. - Query Builder and CRUD Operations: Compose select, join, eager loading, bulk insert, update, and delete statements with the modern select() API. - Alembic Migrations and Async Support: Generate and apply schema migrations with Alembic and run async database operations with AsyncSession for FastAPI applications. - Use Case: When building a FastAPI service backed by PostgreSQL, use this Skill to define typed models, wire up session dependency injection, write N+1-free queries, and manage schema changes through Alembic revisions. ## Quick Start Ask the AI to create SQLAlchemy 2.0 models with relationships and a FastAPI endpoint that queries them using an async session.

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 2.0 models with type hints?▼

Define a DeclarativeBase subclass and annotate fields with Mapped[T] combined with mapped_column() for constraints. SQLAlchemy derives column types and nullability from the annotations, giving you typed models with primary keys, indexes, and defaults.

How to avoid N+1 query problems in SQLAlchemy?▼

Use eager loading with selectinload() for collections or joinedload() for many-to-one relationships in your query options. Nested eager loading like selectinload(User.posts).selectinload(Post.tags) loads related graphs in a fixed number of queries.

Does SQLAlchemy support async database operations?▼

Yes, SQLAlchemy 2.0 provides AsyncSession via create_async_engine and async_sessionmaker, using drivers like asyncpg for PostgreSQL or aiosqlite for SQLite. All queries use await session.execute() and integrate with FastAPI async dependencies.

How do I manage database schema changes with Alembic?▼

Run alembic init, point target_metadata at your Base.metadata in env.py, then use alembic revision --autogenerate to create migrations from model changes. Apply them with alembic upgrade head and roll back with alembic downgrade.

SQLAlchemy vs Django ORM for a FastAPI project?▼

SQLAlchemy is the standard choice for FastAPI since Django ORM is tightly coupled to the Django framework. SQLAlchemy offers explicit session control, a composable select() query API, and first-class async support suited to ASGI applications.

How do I test SQLAlchemy code without a real database?▼

Use an in-memory SQLite engine with StaticPool and create all tables in a pytest fixture that yields a session. Each test function gets a fresh schema, and tables are dropped after the test completes.