Skip to main content

Automatic Pydantic Schemas for SQLAlchemy models

Project description

Schemap

Automatic Pydantic v2 schemas from SQLAlchemy 2.0 ORM models: no more manually mirroring your database columns in Pydantic.

Note: This 0.2.0 release provides the core schema generation functionality. Schema configuration and customization are actively being worked on.

pip install schemap

Quick Start

Define your models with AutoBase. Schemas are generated automatically:

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]

# Four schemas are available as class attributes:
User.Schema           # all columns
User.CreateSchema     # excludes id, defaults (for inserts)
User.UpdateSchema     # all fields optional (for partial updates)
User.PublicSchema     # excludes fields starting with "__"

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

Standalone Usage

Use build_schema without AutoBase:

from schemap import build_schema
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = "users"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]

UserSchema = build_schema(User, "default")

Schema Variants

Variant Filtering Nullability
"default" (.Schema) all columns matches model
"create" (.CreateSchema) excludes PKs, server_defaults, defaults matches model
"update" (.UpdateSchema) excludes PKs all fields Optional[type] = None
"public" (.PublicSchema) excludes __-prefixed columns matches model

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.2.0.tar.gz (8.3 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.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for schemap-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b088c177494739f445ffd4ebdb581d4c7b820e24d0fa14c4f29dbf0184625378
MD5 b18d5ffbe2167f13d91b84d2a39f4c3c
BLAKE2b-256 ebf560d8409de7de32bd1a45e87cad2157f7185cfc18d458ee96c7f9de8039b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: schemap-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f11eabc34e065795aaf8ec3d030784b34aad2433e79d7928bf5d920c29a9e5be
MD5 e07e794f47e36f16226e1b4a371ad13e
BLAKE2b-256 c4f5c47ebeea96efad35de60d3d77f6457b7cfc1909096b2cd48ebaa7ad9a09a

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