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
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
001eacf7c844afe6e608c788eae2c34c48be87deefca9ca27f9ee4c8efd2b55d
|
|
| MD5 |
228a871b4e7b99af5aaacaf6aa38f1da
|
|
| BLAKE2b-256 |
329f031c60385d8e5e9a4ecc720428f86d3b438efdc6341efad77d5ce88cb41f
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84ae0a1122c5e59d3ce534d98497e5a4569777ad83f40abaf2b68695ae430680
|
|
| MD5 |
022302827d555c18a3650a2ac7f065c1
|
|
| BLAKE2b-256 |
b15eec63b9b9853d8993b9c1ec45968937352c56d7d68ec84597d9848f03c025
|