sqlalchemy-python

Guides writing, reviewing, and testing SQLAlchemy 2.x Core and ORM code with correct transaction ownership.

Updated Aug 12, 2026
One-click install
npx skills add https://github.com/schattenspiegel/skill-foundry-skills --skill sqlalchemy-python-schattenspiegel
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: sqlalchemy-python
Source: https://github.com/schattenspiegel/skill-foundry-skills/tree/main/skills/sqlalchemy-python
Command: npx skills add https://github.com/schattenspiegel/skill-foundry-skills --skill sqlalchemy-python-schattenspiegel

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? SQLAlchemy code often fails in production due to misused Sessions, leaked connections, N+1 queries, and confused transaction boundaries. This Skill provides structured guidance for writing, reviewing, debugging, migrating, and testing SQLAlchemy 2.x Core and ORM code with correct resource ownership and concurrency rules. ## Core Features & Use Cases - Resource and Transaction Ownership: Clear rules for Engine, Connection, Session, and Result lifecycles, including commit/rollback scoping with context managers. - Core vs ORM Decision Guidance: Criteria for choosing explicit relational statements versus identity-map and unit-of-work patterns, plus relationship loading strategies to avoid N+1 queries. - Concurrency and Async Safety: Rules for Session/AsyncSession isolation per task, async driver usage, and avoiding sync calls in the event loop. - Use Case: When reviewing a FastAPI endpoint that shares a global Session across requests, use this Skill to restructure it into per-request Session factories with deterministic commit/rollback and proper eager loading. ## Quick Start Ask the assistant to review or write SQLAlchemy 2.x code for your task, specifying whether it uses Core or ORM, the database, and the expected concurrency and scale.

Frequently Asked Questions about sqlalchemy-python

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

FAQPage Schema
How do I manage SQLAlchemy Sessions correctly in Python?▼

Create one Session per request, job, or unit of work using a session factory injected into your code. Never share a Session as a global singleton or across threads and concurrent tasks, since it is a mutable transactional object.

SQLAlchemy Core vs ORM: which should I use?▼

Use Core for explicit relational statements and bulk data-oriented work; use ORM when identity mapping, relationships, and unit-of-work behavior belong to your domain model. Both use 2.x select() and the same transaction semantics.

How do I avoid N+1 queries in SQLAlchemy?▼

Define relationship loading deliberately with joined or select-in loading based on cardinality. Detect N+1 access by counting statements in tests, and watch for detached lazy loads after the Session closes.

Can I share AsyncSession across async tasks?▼

No. AsyncSession is a proxy over the same stateful session model, so use one per task with an async driver. Do not mix synchronous driver calls into the event loop.

Why is testing with SQLite not enough for PostgreSQL behavior?▼

SQLite does not reproduce PostgreSQL semantics for constraints, isolation, and concurrency. Test with the target dialect when its behavior matters, and cover commit, rollback, integrity errors, and transaction visibility.

When should I not use this SQLAlchemy guidance?▼

It does not apply to raw database SQL with no SQLAlchemy boundary, Alembic migration design, DuckDB relations, or general database administration tasks.