Skip to main content

Automatic Pydantic Schemas for SQLAlchemy models

Project description

Schemap

Automatic Pydantic v2 schemas from SQLAlchemy 2.0 ORM models.

pip install schemap

Quick Start

from schemap import AutoBase
from sqlalchemy.orm import Mapped, mapped_column

class User(AutoBase):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    email: Mapped[str]

User.Schema         # all columns
User.CreateSchema   # excludes PKs, server_defaults, defaults
User.UpdateSchema   # all fields Optional (partial updates)
User.PublicSchema   # excludes __-prefixed fields

# Round-trip ORM <-> Pydantic
data = User.CreateSchema(name="Alice", email="alice@example.com")
user = User.from_schema(data)
schema = user.to_schema()

SchemaConfig

Customize generated schemas per model using __schema_config__:

from schemap import AutoBase
from schemap.config import SchemaConfig
from sqlalchemy.orm import Mapped, mapped_column

class User(AutoBase):
    __tablename__ = "users"
    __schema_config__ = SchemaConfig(
        exclude_public=["email"],
        exclude_create=["internal_id"],
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    email: Mapped[str]
    internal_id: Mapped[int]
Option Type Description
exclude_always list[str] Exclude from all schema variants
exclude_create list[str] Exclude from CreateSchema only
exclude_update list[str] Exclude from UpdateSchema only
exclude_public list[str] Exclude from PublicSchema only
field_overrides dict[str, Any] Override a field's Python type
required_always list[str] Force fields to be required
optional_always list[str] Force fields to be optional

Standalone

from schemap import build_schema
from schemap.config import SchemaConfig

UserSchema = build_schema(User, "create", config=SchemaConfig(exclude_create=["internal_id"]))

Requirements

Python ≥ 3.12.7, SQLAlchemy ≥ 2.0.49, Pydantic ≥ 2.13.4.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

schemap-0.3.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

schemap-0.3.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file schemap-0.3.0.tar.gz.

File metadata

  • Download URL: schemap-0.3.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for schemap-0.3.0.tar.gz
Algorithm Hash digest
SHA256 001eacf7c844afe6e608c788eae2c34c48be87deefca9ca27f9ee4c8efd2b55d
MD5 228a871b4e7b99af5aaacaf6aa38f1da
BLAKE2b-256 329f031c60385d8e5e9a4ecc720428f86d3b438efdc6341efad77d5ce88cb41f

See more details on using hashes here.

File details

Details for the file schemap-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: schemap-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for schemap-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 84ae0a1122c5e59d3ce534d98497e5a4569777ad83f40abaf2b68695ae430680
MD5 022302827d555c18a3650a2ac7f065c1
BLAKE2b-256 b15eec63b9b9853d8993b9c1ec45968937352c56d7d68ec84597d9848f03c025

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page