sqlalchemy

Repair and optimize SQLAlchemy 2.x ORM and Core code for performance and transactions.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provide clear, actionable guidance to design, review, and troubleshoot SQLAlchemy 2.x ORM and Core code, reducing runtime errors, N+1 queries, session/transaction misuse, and async/sync pitfalls.

Core Features & Use Cases

  • ORM and Core patterns: Recommend idiomatic SQLAlchemy 2.x usage including DeclarativeBase, Mapped, mapped_column, and when to use Core for bulk or tuned SQL.
  • Query and loading strategies: Identify N+1 problems and propose loader strategies such as selectinload and joinedload to optimize performance.
  • Async and pooling guidance: Advise on async engine/session creation, async_sessionmaker usage, run_sync bridging, and connection pool tuning for production workloads.
  • Debugging and best practices: Diagnose session lifecycle, transaction boundaries, and pooling/exhaustion issues with stepwise remediation suggestions.

Quick Start

Review this SQLAlchemy code for N+1 issues, incorrect transaction/session usage, and async/sync mixing, then provide corrected examples and recommended loader strategies.

Frequently Asked Questions about sqlalchemy

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

FAQPage Schema
How do I fix N+1 query problems in SQLAlchemy 2.x ORM code?

Resolve N+1 queries in SQLAlchemy 2.x by applying loader strategies like selectinload and joinedload to optimize relationship loading and eliminate redundant database queries during code review.

How do I configure async sessions and connection pooling in SQLAlchemy?

Configure async SQLAlchemy by using async_sessionmaker for consistent session creation and tuning connection pool settings to prevent pool exhaustion and avoid async/sync mixing in request handlers.

What is the correct way to manage transaction boundaries with SQLAlchemy Core and ORM?

Manage transaction boundaries in SQLAlchemy by using explicit transaction controls and proper session lifecycle handling to prevent transactional errors and ensure database integrity in backend Python services.

When should I use SQLAlchemy Core instead of the ORM for database operations?

Use SQLAlchemy Core instead of the ORM when executing bulk operations or tuned SQL queries that require direct database interaction without the overhead of object-relational mapping abstractions.

Why does my SQLAlchemy async session throw errors when mixing sync and run_sync operations?

SQLAlchemy async sessions throw errors when mixing sync and run_sync operations due to improper async engine configuration, requiring run_sync bridging to safely execute blocking database calls within async request handlers.

How do I review SQLAlchemy code for performance regressions and session misuse?

Review SQLAlchemy code for performance regressions by checking select() and Session.execute() usage, validating explicit transaction boundaries, and diagnosing session lifecycle issues to ensure production stability.