sqlalchemy and pydantic integration.
Project description
sqldantic
SQLalchemy + pyDANTIC
Example:
from __future__ import annotations
import enum
import ipaddress
from pydantic import BaseModel
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped
from sqldantic import DeclarativeBase, Field, Relationship
class Base(DeclarativeBase):
"""
allowed options are:
metadata
type_annotation_map
json_type
model_config
see https://docs.sqlalchemy.org/en/20/orm/declarative_styles.html
for more information on `metadata` and `type_annotation_map`
see https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict
for more information on `model_config`
`json_type` is JSON by default, but you can change it to JSONB
"""
class OS(str, enum.Enum):
linux = "linux"
windows = "windows"
macos = "macos"
class Info(BaseModel):
os: OS
tags: set[str]
class ClusterBase(Base):
name: Mapped[str]
hosts: Mapped[list[Host]] = Relationship(back_populates="cluster")
class Cluster(ClusterBase, table=True):
id: Mapped[int] = Field(primary_key=True)
class HostBase(Base):
hostname: Mapped[str] = Field(index=True)
address: Mapped[ipaddress.IPv4Address]
info: Mapped[Info]
cluster: Mapped[Cluster] = Relationship(back_populates="hosts")
class Host(HostBase, table=True):
id: Mapped[int] = Field(primary_key=True)
cluster_id: Mapped[int] = Field(ForeignKey("cluster.id"))
Description
Any subclass of DeclarativeBase
is Pydantic Model.
Any subclass of DeclarativeBase
with table=True
is Sqlalchemy Model.
Both Mapped[...]
and "unmapped" formats are supported, but Sqlalchemy needs Mapped
for mypy type checking,
so Mapped
is preferred.
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
sqldantic-0.2.0.tar.gz
(13.8 kB
view details)
Built Distribution
sqldantic-0.2.0-py3-none-any.whl
(15.2 kB
view details)
File details
Details for the file sqldantic-0.2.0.tar.gz
.
File metadata
- Download URL: sqldantic-0.2.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.1 CPython/3.11.5 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4d24d83a77b12ed9d8c84739ff5507868ade018c0295e4cb65f0e5387fd2197 |
|
MD5 | 7461740d3631c2511c268ef8ad245d78 |
|
BLAKE2b-256 | 7f5074d7c878a4b6ce9a9d905ce74d558e95bda2c0156fd53ae22e38fb1b33c2 |
File details
Details for the file sqldantic-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: sqldantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.1 CPython/3.11.5 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d14ccc37b43bedd13361f85afe7404fa0055f0f9a4acb66f7861a4156995531 |
|
MD5 | c1e6e2091480981b6fb9a2a7d3c5df7a |
|
BLAKE2b-256 | 072245094c7a09955be8a51a90b9a2844abb78622261c45b8e7ca906bde14128 |