Skip to main content

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

Project description

Pydantic-SQLAlchemy

Test Coverage Package version

Tools to generate Pydantic models from SQLAlchemy models.

Still experimental.

Install

pip3 install sqlalchemy-dantic

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.2.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_dantic-0.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlalchemy-dantic-0.2.0.tar.gz
  • Upload date:
  • Size: 5.0 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.2.0.tar.gz
Algorithm Hash digest
SHA256 13382027f08fa2960ae018e72e6f1b268286190a70f3292630382caf98e0231a
MD5 24e9fe236544f483d1150fa4f2d06756
BLAKE2b-256 b793b10fc6ebde8778e09aa4096a5cc904a45a105e2e2b1d870d2cffd7b149e7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlalchemy_dantic-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0dabb63fd39622c87bc6e2acf0e78006aaa44d57f8447cd63e71954615a2db08
MD5 c9fc3016def49ca117663fddc8850b94
BLAKE2b-256 8f32b7f69e1f88ddcddee26c6488ccb97a5f88dc331201a3bfe04cc25ab176bb

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