Skip to main content

Tools to generate Pydantic models from SQLAlchemy models.

Source Code forked from tiangolo/pydantic-sqlalchemy

Only support pydantic V2

How to use

Quick example:

from typing import List

from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.orm import Session, declarative_base, relationship, sessionmaker

from sqlalchemy_to_pydantic import sqlalchemy_to_pydantic

Base = declarative_base()

engine = create_engine("sqlite://")


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    nickname = Column(String)

    addresses = relationship(
        "Address", back_populates="user", cascade="all, delete, delete-orphan"
    )


class Address(Base):
    __tablename__ = "addresses"
    id = Column(Integer, primary_key=True)
    email_address = Column(String, nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"))

    user = relationship("User", back_populates="addresses")


PydanticUser = sqlalchemy_to_pydantic(User)
PydanticAddress = sqlalchemy_to_pydantic(Address)


class PydanticUserWithAddresses(PydanticUser):
    addresses: List[PydanticAddress] = []


Base.metadata.create_all(engine)


LocalSession = sessionmaker(bind=engine)

db: Session = LocalSession()

ed_user = User(name="ed", fullname="Ed Jones", nickname="edsnickname")

address = Address(email_address="ed@example.com")
address2 = Address(email_address="eddy@example.com")
ed_user.addresses = [address, address2]
db.add(ed_user)
db.commit()


def test_pydantic_sqlalchemy():
    user = db.query(User).first()
    pydantic_user = PydanticUser.model_validate(user)
    data = pydantic_user.model_dump()
    assert data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "nickname": "edsnickname",
    }
    pydantic_user_with_addresses = PydanticUserWithAddresses.model_validate(user)
    data = pydantic_user_with_addresses.model_dump()
    assert data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "nickname": "edsnickname",
        "addresses": [
            {"email_address": "ed@example.com", "id": 1, "user_id": 1},
            {"email_address": "eddy@example.com", "id": 2, "user_id": 1},
        ],
    }

Metadata

Release files for sqlalchemy-to-pydantic 0.0.8

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

Source distribution (sdist)

Source distribution for sqlalchemy-to-pydantic 0.0.8
File Size Uploaded
sqlalchemy_to_pydantic-0.0.8.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-to-pydantic 0.0.8
File Interpreter ABI Platform
sqlalchemy_to_pydantic-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / sqlalchemy_to_pydantic-0.0.8.tar.gz

Download URL sqlalchemy_to_pydantic-0.0.8.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7af94ecd04c3ca1243975bba5e8e2b2e3928faf9f5ad98a555b0a1b90eec344e
BLAKE2b-256 checksum
How to use checksums
274e0fde8a96fdefcf5b3d8f13eabadc05a89a0be70c2531477e2f80f2a56163
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/3.10.0-1160.24.1.el7.x86_64

Release files / sqlalchemy_to_pydantic-0.0.8-py3-none-any.whl

Download URL sqlalchemy_to_pydantic-0.0.8-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2b13b793b983cc43ec2291bd0dadc731c278017814b98140df8f1c468c4f837
BLAKE2b-256 checksum
How to use checksums
069c0f87dd64e2dc3228ca8593d6ef0be3ab8484500cb87eb9c051c0ecf65840
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/3.10.0-1160.24.1.el7.x86_64

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

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