sqlalchemy-code-review

Review SQLAlchemy 2.0 code for session lifecycle and N+1 query issues.

75|9|Updated Dec 21, 2025
One-click install
npx skills add https://github.com/anderskev/beagle --skill sqlalchemy-code-review
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sqlalchemy-code-review
Source: https://github.com/anderskev/beagle/tree/main/skills/sqlalchemy-code-review
Command: npx skills add https://github.com/anderskev/beagle --skill sqlalchemy-code-review

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps reviewers identify common SQLAlchemy pitfalls around session lifecycle, relationships, and migrations, reducing bugs and lowering review time.

Core Features & Use Cases

  • Session lifecycle, context managers, async sessions: Ensures correct usage patterns across codebases.
  • relationship() usage, lazy loading, N+1 queries: Guides efficient data loading and relationships.
  • select() vs query(), ORM overhead, bulk operations: Encourages modern SQLAlchemy 2.0 patterns.
  • Alembic migration patterns, reversibility: Checks upgrade/downgrade correctness and ordering.

Quick Start

Use the sqlalchemy-code-review skill to review a codebase's SQLAlchemy usage for sessions, relationships, and migrations; refer to the references for anti-patterns and best practices.

Frequently Asked Questions about sqlalchemy-code-review

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

FAQPage Schema
How do I identify N+1 query problems in SQLAlchemy code?

N+1 queries occur when loading related objects triggers separate database queries for each parent record. Use joinedload() or selectinload() in your select() statements to fetch relationships efficiently in a single query, reducing database round trips and improving performance.

What's the correct way to manage SQLAlchemy session lifecycles?

Use context managers (with Session() as session:) to ensure sessions are properly closed and resources released. For async contexts, use async context managers. Avoid sharing sessions across threads or requests; create a fresh session per request or operation to prevent state leaks and connection exhaustion.

How do I write reversible Alembic migrations?

Reversible migrations require explicit upgrade() and downgrade() functions that safely undo schema changes. Test both directions, avoid data loss, and use op.execute() carefully. Ensure migrations preserve data integrity and can roll back without manual intervention for production safety.

Can I use lazy loading with SQLAlchemy 2.0 patterns?

Lazy loading still works but is discouraged in SQLAlchemy 2.0; instead, explicitly load relationships using joinedload(), selectinload(), or subqueryload() in your select() query. This prevents surprise N+1 queries and makes data access patterns explicit and testable.

What relationship() configurations cause common SQLAlchemy issues?

Misconfigured relationship() definitions—missing foreign_keys, incorrect cascade settings, or improper lazy parameters—lead to unexpected query behavior and session errors. Review lazy loading strategy, cascade rules, and back_populates alignment to ensure relationships load and persist data correctly.

How do I handle bulk operations correctly in SQLAlchemy?

Use bulk insert, update, and delete operations via insert(), update(), and delete() constructs instead of looping with add()/merge(). Bulk operations bypass ORM overhead, execute efficiently on the database, and are essential for high-volume data changes while maintaining session consistency.