SQLAlchemy Code Review
Quick Reference
| Issue Type | Reference |
|---|
| Session lifecycle, context managers, async sessions | references/sessions.md |
| relationship(), lazy loading, N+1, joinedload |
references/relationships.md |
| select() vs query(), ORM overhead, bulk ops |
references/queries.md |
| Alembic patterns, reversible migrations, data migrations |
references/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 INLINECODE7 - [ ] Bulk operations use bulkinsert/bulkupdate, not ORM loops
- [ ] Async sessions use proper async context managers
- [ ] Migrations are reversible with INLINECODE8
- [ ] Data migrations use
op.execute() not ORM models - [ ] Migration dependencies properly ordered
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?
- Are relationships configured to avoid N+1 queries?
- Are queries using SQLAlchemy 2.0
select() syntax? - Are all migrations reversible and properly tested?
SQLAlchemy 代码审查
快速参考
references/relationships.md |
| select() vs query()、ORM开销、批量操作 |
references/queries.md |
| Alembic模式、可逆迁移、数据迁移 |
references/migrations.md |
审查清单
- - [ ] 会话使用上下文管理器(with、async with)
- [ ] 不在请求或线程间共享会话
- [ ] 会话正确关闭/清理
- [ ] relationship() 使用适当的 lazy 策略
- [ ] 显式使用 joinedload/selectinload 避免 N+1
- [ ] 循环中无懒加载(N+1 查询)
- [ ] 使用 SQLAlchemy 2.0 select() 语法,而非旧版 query()
- [ ] 批量操作使用 bulkinsert/bulkupdate,而非 ORM 循环
- [ ] 异步会话使用正确的异步上下文管理器
- [ ] 迁移可通过 downgrade() 回滚
- [ ] 数据迁移使用 op.execute() 而非 ORM 模型
- [ ] 迁移依赖关系正确排序
何时加载参考文档
- - 审查会话创建/清理 → sessions.md
- 审查模型关系 → relationships.md
- 审查数据库查询 → queries.md
- 审查 Alembic 迁移文件 → migrations.md
审查问题
- 1. 所有会话是否都通过上下文管理器正确管理?
- 关系配置是否避免了 N+1 查询?
- 查询是否使用 SQLAlchemy 2.0 select() 语法?
- 所有迁移是否可逆且经过正确测试?