sqlalchemy
SQLAlchemy equips Python developers with a robust SQL toolkit and ORM for building type-safe database applications. Master declarative models, relationship mapping, efficient query patterns, and async support using SQLAlchemy 2.0's modern API.
SQLAlchemy teaches Python SQL toolkit and ORM fundamentals with modern 2.0 patterns and best practices.
AI-generated summary based on this skill's SKILL.md
Install
bobmatnyc/claude-mpm-skills/sqlalchemy · repository language: Python
git clone https://github.com/bobmatnyc/claude-mpm-skills
cp -r claude-mpm-skills/toolchains/python/data/sqlalchemy ~/.claude/skills/sqlalchemynpx skillfed install bobmatnyc/claude-mpm-skills/sqlalchemyFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
What is SQLAlchemy and how do I query with it?
SQLAlchemy equips Python developers with a robust SQL toolkit and ORM for building type-safe database applications. You query using the modern 2.0 API with `select()` statements, define models as declarative classes inheriting from `DeclarativeBase`, and execute queries through sessions. SQLAlchemy handles SQL generation, relationship mapping, and database abstraction across multiple backends.
How do I use SQLAlchemy with FastAPI?
SQLAlchemy integrates with FastAPI by creating a database engine, configuring a session factory, and injecting sessions into route handlers via dependency injection. Define your models using declarative syntax, create async sessions with `AsyncSession`, and use them in FastAPI endpoints to perform CRUD operations. This pattern keeps your routes clean and testable.
What are SQLAlchemy 2.0 select query examples?
SQLAlchemy 2.0 uses the `select()` constructor for composable queries. Basic example: `select(User).where(User.age > 18)`. Join multiple tables with `select(User, Post).join(Post)`. Filter with `.where()`, order with `.order_by()`, and limit results with `.limit()`. The API is chainable and type-safe, replacing the legacy query interface.
How do I solve the SQLAlchemy N+1 problem with eager loading?
SQLAlchemy's N+1 problem occurs when loading related objects triggers extra queries. Solve it using eager loading strategies: `selectinload()` executes a second query to load relationships, `joinedload()` uses a single JOIN, and `contains_eager()` works with explicit joins. Import from `sqlalchemy.orm` and apply to your select: `select(User).options(selectinload(User.posts))`.
How do I set up database migrations with Alembic?
SQLAlchemy works with Alembic for schema management. Initialize Alembic with `alembic init`, configure your database URL in `alembic.ini`, and set `sqlalchemy.url`. Define your models, then auto-generate migrations with `alembic revision --autogenerate -m 'message'`. Review the generated migration file, then apply changes with `alembic upgrade head`.
What are SQLAlchemy session management best practices?
SQLAlchemy session management best practices include using context managers (`with Session() as session:`), keeping sessions scoped to request lifecycles in web apps, avoiding session sharing across threads, and committing or rolling back explicitly. Use `sessionmaker` to create a factory, configure connection pooling for performance, and always close sessions to return connections to the pool.
SKILL.md
rendered from the published skill — quoted content, verbatim
SQLAlchemy ORM Skill
progressive_disclosure: entry_point: summary: "Python SQL toolkit and ORM with powerful query builder and relationship mapping" when_to_use: - "When building Python applications with databases" - "When needing complex SQL queries with type safety" - "When working with FastAPI/Flask/Django" - "When needing database migrations (Alembic)" quick_start: - "pip install sqlalchemy" - "Define models with declarative base" -
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 3 files
toolchains/python/data/sqlalchemy/SKILL.md
toolchains/python/data/sqlalchemy/metadata.json
toolchains/python/data/sqlalchemy/references/sql-quality-antipatterns.md