← 返回
未分类 中文

Sqlalchemy Code Review

Reviews SQLAlchemy code for session management, relationships, N+1 queries, and migration patterns. Use when reviewing SQLAlchemy 2.0 code, checking session...
审查SQLAlchemy代码的会话管理、关联关系、N+1查询和迁移模式,用于SQLAlchemy 2.0代码审查和会话检查。
anderskev anderskev 来源
未分类 clawhub v1.1.1 1 版本 100000 Key: 无需
★ 0
Stars
📥 404
下载
💾 1
安装
1
版本
#latest

概述

SQLAlchemy Code Review

Quick Reference

Issue TypeReference
-----------------------
Session lifecycle, context managers, async sessionsreferences/sessions.md
relationship(), lazy loading, N+1, joinedloadreferences/relationships.md
select() vs query(), ORM overhead, bulk opsreferences/queries.md
Alembic patterns, reversible migrations, data migrationsreferences/migrations.md

Review Checklist

  • [ ] Sessions use context managers (with, async with)
  • [ ] No session sharing across requests or threads
  • [ ] Sessions closed/cleaned up properly
  • [ ] relationship() uses appropriate lazy strategy
  • [ ] Explicit joinedload/selectinload to avoid N+1
  • [ ] No lazy loading in loops (N+1 queries)
  • [ ] Using SQLAlchemy 2.0 select() syntax, not legacy query()
  • [ ] Bulk operations use bulk_insert/bulk_update, not ORM loops
  • [ ] Async sessions use proper async context managers
  • [ ] Migrations are reversible with downgrade()
  • [ ] Data migrations use op.execute() not ORM models
  • [ ] Migration dependencies properly ordered

Gates (SQLAlchemy-specific)

Run once per SQLAlchemy-related finding, after you can anchor file:line (see review-verification-protocol) and before the finding ships. If a step’s pass condition is not met, do not assert the finding as written—gather evidence, withdraw, downgrade severity, or rephrase as a question.

Gate 1 — Session scope and lifecycle

StepActionPass condition
-----------------------------------
1aOpen the module where the session is created or injected (not from memory).file:line for Session, sessionmaker, async_session, or the factory/Depends() that yields a session.
1bIf claiming leak, cross-request sharing, or missing cleanup: trace the session’s scope (context manager, try/finally, middleware).Scoped region cited with a file:line range, or withdraw if scope is correct after the read.

Gate 2 — N+1, lazy loading, eager loads

StepActionPass condition
-----------------------------------
2aIdentify the loop or repeated call site (ORM attribute access, execute in a loop).file:line for the loop or hot path.
2bIf claiming N+1: name the relationship or query pattern emitted per iteration.Relationship or per-iteration SQL pattern with file:line, or rephrase as a question if unclear.

Gate 3 — Migrations (Alembic)

StepActionPass condition
-----------------------------------
3aOpen the revision file (e.g. under versions/, or the project’s Alembic layout).Repo-relative path + file:line for revision / upgrade / downgrade.
3bIf claiming broken downgrade() or risky data migration: point at the op.* / op.execute() involved.Snippet or line range in that file for each claimed op, or withdraw.

When to Load References

  • Reviewing session creation/cleanup → sessions.md
  • Reviewing model relationships → relationships.md
  • Reviewing database queries → queries.md
  • Reviewing Alembic migration files → migrations.md

Review Questions

  1. Are all sessions properly managed with context managers?
  2. Are relationships configured to avoid N+1 queries?
  3. Are queries using SQLAlchemy 2.0 select() syntax?
  4. Are all migrations reversible and properly tested?

版本历史

共 1 个版本

  • v1.1.1 当前
    2026-05-03 07:58 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 683 📥 330,430
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 79 📥 182,786
education

Tutorial Docs

anderskev
教程模式——面向学习的指南,通过引导式实践教学。用于编写教程、学习指南、入门指南等。
★ 0 📥 704