Claude Code for SQLAlchemy: Python ORM and SQL Toolkit — Claude Skills 360 Blog
Blog / AI / Claude Code for SQLAlchemy: Python ORM and SQL Toolkit
AI

Claude Code for SQLAlchemy: Python ORM and SQL Toolkit

Published: November 19, 2027
Read time: 5 min read
By: Claude Skills 360

SQLAlchemy provides both a Pythonic ORM and a low-level SQL expression language. pip install sqlalchemy. from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship, Session. ORM model: class Base(DeclarativeBase): pass. class User(Base): __tablename__="users", id: Mapped[int] = mapped_column(primary_key=True), email: Mapped[str] = mapped_column(unique=True), orders: Mapped[list["Order"]] = relationship(back_populates="user"). Create engine: engine = create_engine("postgresql://user:pass@host/db", echo=False, pool_size=10). Create tables: Base.metadata.create_all(engine). Session: Session = sessionmaker(engine), with Session() as s: s.add(user); s.commit(). Query 2.0: stmt = select(User).where(User.active == True).order_by(User.name), users = session.scalars(stmt).all(). Insert: session.execute(insert(User).values(email="[email protected]")). Update: session.execute(update(User).where(User.id==1).values(active=False)). Delete: session.execute(delete(User).where(User.id==1)). Join: select(User, Order).join(Order.user). Eager loading: select(User).options(selectinload(User.orders)). Async: from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession. Engine: engine = create_async_engine("postgresql+asyncpg://..."). Bulk: session.execute(insert(User), [{"email": e} for e in emails]). Raw SQL: session.execute(text("SELECT * FROM users WHERE id=:id"), {"id":1}). Claude Code generates SQLAlchemy ORM models, async database services, migration scripts, and query builders.

CLAUDE.md for SQLAlchemy

## SQLAlchemy Stack
- Version: sqlalchemy >= 2.0, psycopg2/asyncpg for PostgreSQL
- ORM: DeclarativeBase | Mapped[T] | mapped_column(primary_key, unique, index)
- Session: sessionmaker(engine) | with Session() as s: ... s.commit()
- Query 2.0: select(Model).where(...).order_by(...) | session.scalars(stmt).all()
- Eager: selectinload(rel) | joinedload(rel) in options()
- Async: create_async_engine | AsyncSession | async with session:
- Bulk: session.execute(insert(Model), list_of_dicts)
- Migrations: alembic init alembic | alembic revision --autogenerate

SQLAlchemy ORM Pipeline

# db/sqlalchemy_pipeline.py — Python ORM and SQL toolkit with SQLAlchemy 2.0
from __future__ import annotations
from datetime import datetime
from typing import Optional, Any
import uuid

from sqlalchemy import (
    create_engine, String, Integer, Float, Boolean, DateTime, Text,
    ForeignKey, UniqueConstraint, Index, CheckConstraint,
    select, insert, update, delete, func, text, event,
)
from sqlalchemy.orm import (
    DeclarativeBase, Mapped, mapped_column, relationship,
    Session, sessionmaker, selectinload, joinedload,
)
from sqlalchemy.pool import QueuePool


# ── 1. Models ─────────────────────────────────────────────────────────────────

class Base(DeclarativeBase):
    pass


class User(Base):
    """User account with profile and orders."""
    __tablename__ = "users"
    __table_args__ = (
        UniqueConstraint("email", name="uq_user_email"),
        Index("ix_user_email_active", "email", "active"),
    )

    id:         Mapped[int]          = mapped_column(primary_key=True)
    email:      Mapped[str]          = mapped_column(String(255), nullable=False)
    name:       Mapped[str]          = mapped_column(String(120))
    active:     Mapped[bool]         = mapped_column(Boolean, default=True)
    created_at: Mapped[datetime]     = mapped_column(DateTime, default=datetime.utcnow)
    role:       Mapped[str]          = mapped_column(String(50), default="user")

    # Relationships
    orders: Mapped[list["Order"]]    = relationship(back_populates="user",
                                                    cascade="all, delete-orphan")
    profile: Mapped[Optional["UserProfile"]] = relationship(back_populates="user",
                                                             uselist=False,
                                                             cascade="all, delete-orphan")

    def __repr__(self) -> str:
        return f"<User id={self.id} email={self.email}>"


class UserProfile(Base):
    """Optional user profile — one-to-one with User."""
    __tablename__ = "user_profiles"

    id:      Mapped[int] = mapped_column(primary_key=True)
    user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), unique=True)
    bio:     Mapped[Optional[str]] = mapped_column(Text)
    score:   Mapped[float] = mapped_column(Float, default=0.0)

    user: Mapped["User"] = relationship(back_populates="profile")


class Product(Base):
    """Product catalog."""
    __tablename__ = "products"
    __table_args__ = (
        CheckConstraint("price >= 0", name="ck_product_price_positive"),
    )

    id:          Mapped[int]   = mapped_column(primary_key=True)
    sku:         Mapped[str]   = mapped_column(String(50), unique=True)
    name:        Mapped[str]   = mapped_column(String(255))
    price:       Mapped[float] = mapped_column(Float)
    stock:       Mapped[int]   = mapped_column(Integer, default=0)
    active:      Mapped[bool]  = mapped_column(Boolean, default=True)

    order_items: Mapped[list["OrderItem"]] = relationship(back_populates="product")


class Order(Base):
    """Purchase order."""
    __tablename__ = "orders"

    id:         Mapped[int]      = mapped_column(primary_key=True)
    user_id:    Mapped[int]      = mapped_column(ForeignKey("users.id"))
    total:      Mapped[float]    = mapped_column(Float, default=0.0)
    status:     Mapped[str]      = mapped_column(String(50), default="pending")
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    user:  Mapped["User"]           = relationship(back_populates="orders")
    items: Mapped[list["OrderItem"]] = relationship(back_populates="order",
                                                     cascade="all, delete-orphan")


class OrderItem(Base):
    """Order line item."""
    __tablename__ = "order_items"

    id:         Mapped[int]   = mapped_column(primary_key=True)
    order_id:   Mapped[int]   = mapped_column(ForeignKey("orders.id"))
    product_id: Mapped[int]   = mapped_column(ForeignKey("products.id"))
    quantity:   Mapped[int]   = mapped_column(Integer, default=1)
    unit_price: Mapped[float] = mapped_column(Float)

    order:   Mapped["Order"]   = relationship(back_populates="items")
    product: Mapped["Product"] = relationship(back_populates="order_items")


# ── 2. Engine and Session factory ────────────────────────────────────────────

def create_db_engine(
    url:        str,
    echo:       bool = False,
    pool_size:  int  = 10,
    max_overflow: int = 20,
):
    """
    Create SQLAlchemy 2.0 engine with connection pool.
    url: e.g. "postgresql://user:pass@localhost/mydb" or "sqlite:///./db.sqlite"
    """
    return create_engine(
        url,
        echo=echo,
        poolclass=QueuePool,
        pool_size=pool_size,
        max_overflow=max_overflow,
        pool_pre_ping=True,      # Verify connection health before use
    )


def make_session_factory(engine):
    """Return a sessionmaker for the given engine."""
    return sessionmaker(engine, autoflush=False, autocommit=False)


# ── 3. CRUD operations ────────────────────────────────────────────────────────

def create_user(
    session: Session,
    email:   str,
    name:    str,
    role:    str = "user",
) -> User:
    """Create and persist a new user. Raises IntegrityError if email exists."""
    user = User(email=email, name=name, role=role)
    session.add(user)
    session.flush()   # Populate user.id without committing
    return user


def get_user_by_email(session: Session, email: str) -> User | None:
    """Fetch user by email with profile eagerly loaded."""
    stmt = (
        select(User)
        .where(User.email == email)
        .options(joinedload(User.profile))
    )
    return session.scalars(stmt).first()


def get_users_with_orders(
    session:   Session,
    active:    bool = True,
    limit:     int  = 100,
    offset:    int  = 0,
) -> list[User]:
    """Fetch users with all orders eagerly loaded (avoids N+1)."""
    stmt = (
        select(User)
        .where(User.active == active)
        .options(selectinload(User.orders).selectinload(Order.items))
        .order_by(User.created_at.desc())
        .limit(limit)
        .offset(offset)
    )
    return session.scalars(stmt).all()


def update_user_status(
    session:   Session,
    user_id:   int,
    active:    bool,
) -> int:
    """Bulk-update active status. Returns rows affected."""
    stmt = (
        update(User)
        .where(User.id == user_id)
        .values(active=active)
        .execution_options(synchronize_session="fetch")
    )
    result = session.execute(stmt)
    return result.rowcount


def delete_user(session: Session, user_id: int) -> bool:
    """Delete user (cascades to orders and profile)."""
    user = session.get(User, user_id)
    if user:
        session.delete(user)
        return True
    return False


# ── 4. Aggregation queries ────────────────────────────────────────────────────

def sales_by_user(
    session: Session,
    limit:   int = 10,
) -> list[dict]:
    """Top N users by total order value."""
    stmt = (
        select(
            User.id,
            User.email,
            func.count(Order.id).label("order_count"),
            func.sum(Order.total).label("total_revenue"),
        )
        .join(Order, Order.user_id == User.id)
        .where(User.active == True)
        .group_by(User.id, User.email)
        .order_by(func.sum(Order.total).desc())
        .limit(limit)
    )
    rows = session.execute(stmt).all()
    return [{"id": r.id, "email": r.email,
             "order_count": r.order_count, "total_revenue": r.total_revenue}
            for r in rows]


def product_inventory_summary(session: Session) -> list[dict]:
    """Products with stock level and total sold quantity."""
    stmt = (
        select(
            Product.sku,
            Product.name,
            Product.stock,
            func.coalesce(func.sum(OrderItem.quantity), 0).label("total_sold"),
        )
        .outerjoin(OrderItem, OrderItem.product_id == Product.id)
        .where(Product.active == True)
        .group_by(Product.id, Product.sku, Product.name, Product.stock)
        .order_by(func.sum(OrderItem.quantity).desc().nullslast())
    )
    rows = session.execute(stmt).all()
    return [r._asdict() for r in rows]


# ── 5. Bulk operations ────────────────────────────────────────────────────────

def bulk_insert_users(
    session:  Session,
    records:  list[dict],
) -> int:
    """
    High-throughput insert using Core (no ORM overhead).
    records: [{"email": ..., "name": ..., "role": ...}, ...]
    Returns number of inserted rows.
    """
    if not records:
        return 0
    result = session.execute(insert(User), records)
    return result.rowcount


def upsert_products(
    session:  Session,
    products: list[dict],
) -> None:
    """
    Upsert products by SKU.
    For PostgreSQL: uses ON CONFLICT DO UPDATE.
    """
    from sqlalchemy.dialects.postgresql import insert as pg_insert
    stmt = (
        pg_insert(Product)
        .values(products)
        .on_conflict_do_update(
            index_elements=["sku"],
            set_={"price": pg_insert(Product).excluded.price,
                  "stock": pg_insert(Product).excluded.stock},
        )
    )
    session.execute(stmt)


# ── 6. Event listeners (audit logging) ────────────────────────────────────────

def attach_audit_listener(session_factory):
    """
    Attach SQLAlchemy event listeners to log all INSERT/UPDATE/DELETE.
    Useful for audit trails without modifying model code.
    """
    @event.listens_for(session_factory, "after_flush")
    def log_changes(session, flush_context):
        for obj in session.new:
            print(f"[AUDIT] INSERT {type(obj).__name__} id={getattr(obj,'id','?')}")
        for obj in session.dirty:
            print(f"[AUDIT] UPDATE {type(obj).__name__} id={getattr(obj,'id','?')}")
        for obj in session.deleted:
            print(f"[AUDIT] DELETE {type(obj).__name__} id={getattr(obj,'id','?')}")


# ── Demo ──────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    print("SQLAlchemy 2.0 ORM Demo")
    print("=" * 50)

    # In-memory SQLite for demo
    engine   = create_db_engine("sqlite:///:memory:", echo=False)
    Base.metadata.create_all(engine)
    Session_ = make_session_factory(engine)

    with Session_() as s:
        # Create users
        alice = create_user(s, "[email protected]", "Alice", "admin")
        bob   = create_user(s, "[email protected]", "Bob")

        # Create products
        s.add_all([
            Product(sku="P001", name="Widget A", price=29.99, stock=100),
            Product(sku="P002", name="Widget B", price=49.99, stock=50),
        ])
        s.flush()

        # Create orders
        p1 = s.scalars(select(Product).where(Product.sku=="P001")).first()
        order = Order(user_id=alice.id, total=59.98, status="completed")
        s.add(order)
        s.flush()
        s.add(OrderItem(order_id=order.id, product_id=p1.id, quantity=2, unit_price=29.99))

        s.commit()

        # Queries
        user = get_user_by_email(s, "[email protected]")
        print(f"\nFetched: {user}")

        users = get_users_with_orders(s, active=True)
        print(f"Active users: {len(users)}")

        top_users = sales_by_user(s, limit=5)
        print(f"\nTop users by revenue:")
        for u in top_users:
            print(f"  {u['email']}: {u['order_count']} orders, ${u['total_revenue']:.2f}")

        # Bulk insert
        records = [{"email": f"user{i}@example.com", "name": f"User {i}", "active": True}
                   for i in range(10)]
        n = bulk_insert_users(s, records)
        s.commit()
        print(f"\nBulk inserted {n} users")

        total = s.scalar(select(func.count(User.id)))
        print(f"Total users: {total}")

For the Django ORM alternative when a full-stack web framework is needed — Django provides admin UI, forms, and auth while SQLAlchemy 2.0’s select().options(selectinload()) pattern for relationship loading, execution_options(synchronize_session="fetch") for bulk updates, and the Core expression language for cross-database SQL give fine-grained control over query shape that Django’s ORM obscures, making SQLAlchemy the preferred choice for data-heavy microservices and tools where query efficiency is critical. For the raw psycopg2 alternative when direct SQL is preferred — psycopg2 provides no connection pooling or schema management while SQLAlchemy’s QueuePool with pool_pre_ping, metadata.create_all, and alembic migration suite replace an entire infrastructure stack, and the Unit of Work session pattern batches all inserts/updates/deletes in one transaction flush with automatic rollback on exception. The Claude Skills 360 bundle includes SQLAlchemy skill sets covering DeclarativeBase ORM models, relationships with cascade, session transactions, SQLAlchemy 2.0 select/insert/update/delete, eager loading with selectinload and joinedload, aggregation queries with func, bulk inserts, PostgreSQL upsert, event listeners, and async sessions with AsyncEngine. Start with the free tier to try ORM code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free