Skip to main content

Seamlessly use Pydantic 2 models in a Postgres SQLAlchemy model

Project description

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))

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

pydantic_sqlalchemy_deco-0.0.4.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_sqlalchemy_deco-0.0.4.tar.gz.

File metadata

  • Download URL: pydantic_sqlalchemy_deco-0.0.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pydantic_sqlalchemy_deco-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f9fb1d19559b8166e803cf2ae706af256736a15393eabb36dc44f4bff53955af
MD5 22ad3df6e7b396bf2bfced1ac639af93
BLAKE2b-256 75dffd232b82966f29ee4e384ca314170f34a9728267f228eefe9c4242c38403

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_sqlalchemy_deco-0.0.4.tar.gz:

Publisher: python-publish.yml on kevinhikaruevans/pydantic-sqlalchemy-type-decorator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7db4041211a551f99ffdfb16ce49090b2a49136171a5b40642f30d2695fd505f
MD5 b258ff17c56938f0f524db6414b45052
BLAKE2b-256 f44f2d3ed1eaf034f3030ed5f241b49df44e7553b8fd2a397f36a4b566eadb5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_sqlalchemy_deco-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on kevinhikaruevans/pydantic-sqlalchemy-type-decorator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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