Tools to convert SQLAlchemy models to Pydantic models, forked from tiangolo/pydantic-sqlalchemy
Project description
pyd-sqa
Pydantic-SQLAlchemy
Tools to generate Pydantic models from SQLAlchemy models, forked from pydantic-sqlalchemy
Still experimental.
How to use
Quick example:
from typing import List
from pyd_sqa import pyd_sqa
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 = pyd_sqa(User)
PydanticAddress = pyd_sqa(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_pyd_sqa():
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},
],
}
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
pyd_sqa-0.0.9.tar.gz
(3.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyd_sqa-0.0.9.tar.gz.
File metadata
- Download URL: pyd_sqa-0.0.9.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.9.18 Linux/6.2.0-1012-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fc01c9f93f2e795facf43c8476b8418fa7ced81fe5f63d71cb5dd56e6b2f349
|
|
| MD5 |
b917850e0ee5db2d1b931ab4f2b17176
|
|
| BLAKE2b-256 |
498640c3eb9b5ac3ce77bc71f4a5fae1e45f75a64d7c71a2be4261ec36ef4262
|
File details
Details for the file pyd_sqa-0.0.9-py3-none-any.whl.
File metadata
- Download URL: pyd_sqa-0.0.9-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.9.18 Linux/6.2.0-1012-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c90029fd9688692d007b74e5d705456662581a1fea07e33f95d267fe02e1bcc3
|
|
| MD5 |
f1dcd6e777983441319fa00e55c65e5e
|
|
| BLAKE2b-256 |
f6dd18700ac381dea879409ecc507aa932bde867b22b1c8063371b6abeb58fb6
|