Skip to main content

Tools to convert SQLAlchemy models to Pydantic models.Thanks project 'pydantic-sqlalchemy'.

Project description

Pydantic-SQLAlchemy

Test Publish Coverage Package version

Tools to generate Pydantic models from SQLAlchemy models.

Still experimental.

How to use

Quick example:

from typing import List

from sqlalchemy_dantic import sqlalchemy_to_pydantic
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship, sessionmaker

Base = declarative_base()

engine = create_engine("sqlite://", echo=True)


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,name="PydanticUser")
PydanticAddress = sqlalchemy_to_pydantic(Address,name="PydanticAddress")


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_sqlalchemy_dantic():
    user = db.query(User).first()
    pydantic_user = PydanticUser.from_orm(user)
    data = pydantic_user.dict()
    assert data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "nickname": "edsnickname",
    }
    pydantic_user_with_addresses = PydanticUserWithAddresses.from_orm(user)
    data = pydantic_user_with_addresses.dict()
    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},
        ],
    }

Release Notes

add sub Schema .

License

This project is licensed under the terms of the MIT license.

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-dantic-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_dantic-0.1.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-dantic-0.1.1.tar.gz.

File metadata

  • Download URL: sqlalchemy-dantic-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.5 Linux/5.10.29-amd64-desktop

File hashes

Hashes for sqlalchemy-dantic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e96aff6270c4a1b1ab4a1cff04000a71b2ea5865dba5b7bb3671e06c32558485
MD5 b2a6173a7e8eb93f27733b2e3e533602
BLAKE2b-256 2f7a727ab72df73530a6e9c3559e0b3cb423a46696af9df0b058897a4456e5d0

See more details on using hashes here.

File details

Details for the file sqlalchemy_dantic-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_dantic-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.5 Linux/5.10.29-amd64-desktop

File hashes

Hashes for sqlalchemy_dantic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2cd3f087b1e8d5180ee650f73c75d4537b76389fac78fa66e8abb050744ca8a9
MD5 aaca1114f86c36a62072dfbd4bcb5e41
BLAKE2b-256 978ef58c49ee4c0e451aae8d5872d10f7b935144d474a504a88934099ddfe60e

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