Tools to convert SQLAlchemy models to Pydantic models.Thanks project 'pydantic-sqlalchemy'.
Project description
Pydantic-SQLAlchemy
Tools to generate Pydantic models from SQLAlchemy models.
Still experimental.
How to use
Quick example:
from typing import List
from pydantic_sqlalchemy 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_pydantic_sqlalchemy():
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sqlalchemy-dantic-0.1.0.tar.gz
.
File metadata
- Download URL: sqlalchemy-dantic-0.1.0.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77c76d45f9fb1bc329368e7e630ef50b0af67679eeb8a1b48a659c0817f21599 |
|
MD5 | 64585f3d89430a3797b831a6be7a729a |
|
BLAKE2b-256 | 5b8f874d3012b43bade73a405a6581862bc00016ab411676015556fe8a263dd6 |
File details
Details for the file sqlalchemy_dantic-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_dantic-0.1.0-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dc288671fb511b2cc11f9fc4decc5041093448b903b802af512a5e2275338d5 |
|
MD5 | 9e9a222aae402d7d828e7050ad753dff |
|
BLAKE2b-256 | 73168587d37beb98e754e27633e34f0701216f1390dc421dc6d9070496d9ae1d |