sqlalchemy-alembic-expert-best-practices-code-review

Reviews SQLAlchemy models and Alembic migrations against safe schema-change best practices.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Unsafe database migrations can lock tables, block writes, and cause downtime in production. This Skill provides opinionated rules for writing and reviewing SQLAlchemy ORM models and Alembic migrations so schema changes, indexes, and constraints are applied without blocking operations. ## Core Features & Use Cases - Concurrent Index Rules: Enforces postgresql_concurrently=True with autocommit blocks for all index creation and deletion. - Safe Constraint Patterns: Splits unique constraints, foreign keys, and check constraints into NOT VALID creation plus separate validation steps. - Multi-Step Column Type Changes: Guides column type migrations through add-column, backfill, and swap phases to avoid table rewrites. - Use Case: When reviewing a pull request that adds a unique constraint on a large users table, apply the unique-constraint rule to rewrite it as a concurrent unique index followed by a constraint using that index. ## Quick Start Review my Alembic migration and SQLAlchemy models for unsafe index, constraint, and column type change patterns.

Frequently Asked Questions about sqlalchemy-alembic-expert-best-practices-code-review

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

FAQPage Schema
How do I create an index in Alembic without locking the table?▼

Create indexes in Alembic using postgresql_concurrently=True inside an autocommit block via op.get_context().autocommit_block(). This prevents blocking writes during index creation or deletion on PostgreSQL tables.

How to add a unique constraint on a large table in SQLAlchemy?▼

Add unique constraints safely by first creating a unique index concurrently, then attaching the constraint with postgresql_using_index in a separate migration. This avoids blocking reads and writes during constraint creation on large tables.

Does Alembic support adding foreign keys without validation?▼

Yes, Alembic supports postgresql_not_valid=True on create_foreign_key, which adds the constraint without scanning existing rows. Validate the data later in a separate migration using ALTER TABLE ... VALIDATE CONSTRAINT.

Why does changing a column type in Alembic cause downtime?▼

Directly altering a column type with op.alter_column can rewrite the entire table and hold locks. The safe approach adds a new column, backfills data, switches reads and writes, then drops the old column across multiple migrations.

How many columns should a non-unique index have in PostgreSQL?▼

Limit non-unique indexes to a maximum of three columns for efficiency. Prioritize the most selective columns to improve query performance and reduce storage overhead.