Skip to main content

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.

Metadata

Release files for sqlalchemy-dantic 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 sqlalchemy-dantic 0.2.0
File Size Uploaded
sqlalchemy-dantic-0.2.0.tar.gz 5.0 kB Details

Built distribution (wheel)

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

Total release size: 9.8 kB

Release files / sqlalchemy-dantic-0.2.0.tar.gz

Download URL sqlalchemy-dantic-0.2.0.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
13382027f08fa2960ae018e72e6f1b268286190a70f3292630382caf98e0231a
BLAKE2b-256 checksum
How to use checksums
b793b10fc6ebde8778e09aa4096a5cc904a45a105e2e2b1d870d2cffd7b149e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.5 Linux/5.10.29-amd64-desktop

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

Download URL sqlalchemy_dantic-0.2.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0dabb63fd39622c87bc6e2acf0e78006aaa44d57f8447cd63e71954615a2db08
BLAKE2b-256 checksum
How to use checksums
8f32b7f69e1f88ddcddee26c6488ccb97a5f88dc331201a3bfe04cc25ab176bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.5 Linux/5.10.29-amd64-desktop

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

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