Skip to main content

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.

Metadata

Release files for sqldantic 0.2.0

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

Source distribution (sdist)

Source distribution for sqldantic 0.2.0
File Size Uploaded
sqldantic-0.2.0.tar.gz 13.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqldantic 0.2.0
File Interpreter ABI Platform
sqldantic-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.0 kB

Release files / sqldantic-0.2.0.tar.gz

Download URL sqldantic-0.2.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d4d24d83a77b12ed9d8c84739ff5507868ade018c0295e4cb65f0e5387fd2197
BLAKE2b-256 checksum
How to use checksums
7f5074d7c878a4b6ce9a9d905ce74d558e95bda2c0156fd53ae22e38fb1b33c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.1 CPython/3.11.5 Darwin/23.4.0

Release files / sqldantic-0.2.0-py3-none-any.whl

Download URL sqldantic-0.2.0-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d14ccc37b43bedd13361f85afe7404fa0055f0f9a4acb66f7861a4156995531
BLAKE2b-256 checksum
How to use checksums
072245094c7a09955be8a51a90b9a2844abb78622261c45b8e7ca906bde14128
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.1 CPython/3.11.5 Darwin/23.4.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

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