Skip to main content

Tools to convert SQLAlchemy models to Pydantic models

Project description

Tools to generate Pydantic models from SQLAlchemy models.

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},
        ],
    }

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

sqlalchemy_to_pydantic-0.0.4.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_to_pydantic-0.0.4-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlalchemy_to_pydantic-0.0.4.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-82-generic

File hashes

Hashes for sqlalchemy_to_pydantic-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f3276f97bb25a8752ad02aee7095316e5e4481bfc9aed22485a48d897d6b710d
MD5 011f282a17f2f1e87222cc372fcae623
BLAKE2b-256 fdd42c10383636668a95d82839154097428016ba02b89d8b42600bbd70eaf1c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlalchemy_to_pydantic-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d0e8db1cf8b43b574dc176538a8d8ce0b730c45f074d4c82da4008388f32873b
MD5 59ed65900e101d4fdd66cc93d869acb8
BLAKE2b-256 68a40d23e6564b9769a40534a7a949bffd5fd8e22ee87ffad3a338a2b7bed445

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page