Skip to main content

pydantic-sqlalchemy-type-decorator

Seamlessly use Pydantic 2 models in a Postgres SQLAlchemy model

Installation

The package is available on pypi.

You can install this package using pip:

pip install pydantic_sqlalchemy_deco

Requirements

This library requires:

  • Pydantic 2+
  • SQLAlchemy (2.0+?)
  • Postgres with JSONB support

Other versions may be supported as well, but please open an issue if you run into any problems.

Usage

Given some Pydantic model called MyCustomModel, you can specify your columns like:

from pydantic_sqlalchemy_deco.decorator import PydanticJSON

...

# Define Pydantic model somewhere:
class MyCustomModel(BaseModel):
    custom_id: int = 0


# Define your SQLAlchemy model:
class MyEntry(Base):

    custom_data: Mapped[MyCustomModel] = mapped_column(PydanticJSON(MyCustomModel))

Then, you can use the custom_data column as you'd expect:

entry = MyEntry(
    custom_data=MyCustomModel(custom_id=1234)
)

...

print(entry.custom_data.custom_id)

BaseModel changes

If your BaseModel has custom serializers, you can specify this using the T parameter:

    custom_data: Mapped[MyCustomModel] = mapped_column(PydanticJSON[CoolerBaseModel](MyCustomModel))

Release files for pydantic-sqlalchemy-deco 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-sqlalchemy-deco 0.0.4
File Size Uploaded
pydantic_sqlalchemy_deco-0.0.4.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-sqlalchemy-deco 0.0.4
File Interpreter ABI Platform
pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size:8.6 kB

Release files / pydantic_sqlalchemy_deco-0.0.4.tar.gz

Download URL pydantic_sqlalchemy_deco-0.0.4.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f9fb1d19559b8166e803cf2ae706af256736a15393eabb36dc44f4bff53955af
BLAKE2b-256 checksum
How to use checksums
75dffd232b82966f29ee4e384ca314170f34a9728267f228eefe9c4242c38403
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 18, 2024.

Transparency log

Release files / pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl

Download URL pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7db4041211a551f99ffdfb16ce49090b2a49136171a5b40642f30d2695fd505f
BLAKE2b-256 checksum
How to use checksums
f44f2d3ed1eaf034f3030ed5f241b49df44e7553b8fd2a397f36a4b566eadb5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 18, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page