ddtully/dotfiles/python-sqlalchemy
Claude Code and agent skill by ddtully/dotfiles
Trust tier 2
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
SQLAlchemy ORM patterns for Python database access. Use when defining models, writing queries, implementing upserts, working with JSON columns, or managing database sessions.
Install
npx vskill@latest install ddtully/dotfiles/python-sqlalchemySKILL.md
The same SKILL.md was published earlier as ddtully/dotfiles/python-sqlalchemy.
Quick Reference
| Task | SQLAlchemy 2.0 Pattern | |
|---|---|---|
| Define model | class User(Base): ... with Mapped[...] + mapped_column(...) | |
| Open session | SessionLocal = sessionmaker(bind=engine) | |
| Query rows | session.scalars(select(User).where(...)).all() | |
| Query one | session.scalar(select(User).where(...)) | |
| Get by PK | session.get(User, user_id) | |
| Insert/update | session.add(obj) then session.commit() | |
| Delete | session.delete(obj) then session.commit() | |
| Eager loading | select(User).options(selectinload(User.posts)) | |
| Pagination | .order_by(...).limit(limit).offset(offset) | |
| Upsert (Postgres) | insert(...).on_conflict_do_update(...) |
| Session Rule | Why | |
|---|---|---|
| One session per request/unit of work | Prevent state leaks | |
| Always rollback on exceptions | Keep transaction clean | |
| Commit explicitly | Avoid accidental writes | |
| Refresh after commit when needed | Ensure DB-generated fields loaded |
When to Use This Skill
Use for SQLAlchemy ORM and query design:
- Defining typed models with SQLAlchemy 2.0 APIs
- Building CRUD and filtering queries
- Managing sync or async sessions and transactions
- Solving N+1 issues with relationship loading strategies
- Implementing Postgres upserts and JSONB queries
Related skills:
- For Python fundamentals: see
python-fundamentals - For async patterns: see
python-asyncio - For backend architecture: see
python-backend
SQLAlchemy 2.0 Practical Guide
Base Setup
Sync Engine and Session
Async Engine and Session
Model Patterns
Typed Model Example
Modeling Guidelines
- Use
Mapped[T]andmapped_column(...)for all mapped fields - Add DB-level constraints (
unique,index, foreign keys) instead of app-only checks - Keep relationship names explicit (
author/posts) and mirrored withback_populates - Prefer server timestamps (
server_default=func.now()) for write consistency
Query Patterns
Select and Filter
Single Row and Existence
Relationship Loading
- Use
selectinloadfor one-to-many collections in list pages - Use
joinedloadfor many-to-one or when row explosion is acceptable - Avoid lazy loading in response serialization paths
Transactions and Session Lifecycle
Sync Unit of Work
Safe Session Scope
Async Session Scope
Upserts (PostgreSQL)
- Prefer conflict target by unique index/constraint
- Keep upserts idempotent and explicit about updated columns
Performance Checklist
- Select only needed columns for large list endpoints
- Add indexes for common filters and sort keys
- Use eager loading to avoid N+1 query bursts
- Paginate unbounded list queries
- Inspect generated SQL and query plans for hot paths
Common Pitfalls
- Mixing sync session usage in async code paths
- Reusing one session across unrelated requests
- Forgetting rollback after exceptions
- Returning ORM objects after session close with lazy attributes unresolved
- Using
query(...)patterns in new code instead ofselect(...)
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/.agent_skills/python-sqlalchemy/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/ddtully/dotfiles/python-sqlalchemy)