Skip to content
Back to skills

Sqlalchemy Patterns

ASecurity

Applies SQLAlchemy 2 patterns for sessions, typed models, relationship loading and Alembic migrations, and removes legacy 1.x habits.

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 15, 2026
ai-agentssqlapidatabase

Works with

  • api

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill sqlalchemy-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Sqlalchemy Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Sqlalchemy Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-sqlalchemy-patterns/badge)](https://www.skillsdirectory.com/skills/workspace-templates-sqlalchemy-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: sqlalchemy-patterns
description: Applies SQLAlchemy 2 patterns for sessions, typed models, relationship loading and Alembic migrations, and removes legacy 1.x habits.
---

# SQLAlchemy patterns

Use the 2.0 API: typed models, `select()` statements and short, explicit sessions.

## Steps

1. Define models with `DeclarativeBase`, `Mapped[...]` and `mapped_column(...)`. Give each foreign key an index, set `nullable` on purpose, and name constraints through a naming convention so Alembic can drop them later.
2. Create one engine per process and a `sessionmaker`. Open a session per unit of work (a request or a job) with a context manager: `with Session(engine) as session, session.begin():` so it commits on success and rolls back on error. Never share a session between threads or tasks.
3. Query with `session.execute(select(User).where(...))` and `.scalars()`. Do not use the legacy `Query` API in new code.
4. Choose loading per relationship and per query: `selectinload` for collections, `joinedload` for many-to-one, `raiseload("*")` in tests to make surprise lazy loads fail. In async code lazy loading is not allowed at all, so load what you need up front.
5. Do bulk work in the database: `update()`, `delete()` and `insert()` statements, not loops over objects. Stream big reads with `yield_per=1000`.
6. Handle concurrency with `with_for_update()` or an atomic `UPDATE ... SET n = n + 1`.
7. For migrations run `alembic revision --autogenerate -m "..."`, then read the file: autogenerate misses renames (it emits drop and add), server defaults and some type changes. Edit by hand, and add a working `downgrade`.

## Rules

- Never commit an autogenerated migration unread.
- Keep session handling out of models; pass the session in.

## Report

What changed, query counts before and after for any loading fix, and the migration reviewed.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.