Tools to convert SQLAlchemy models to Pydantic models
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)
PydanticAddress = sqlalchemy_to_pydantic(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_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
Latest Changes
0.0.8
- ⬆️ Upgrade
importlib-metadata
to 3.0.0. PR #22 by @tiangolo. - 👷 Add GitHub Action latest-changes. PR #20 by @tiangolo.
- 💚 Fix GitHub Actions Poetry setup. PR #21 by @tiangolo.
0.0.7
- Update requirements of
importlib-metadata
to support the latest version2.0.0
. PR #11.
0.0.6
- Add support for SQLAlchemy extended types like sqlalchemy-utc: UtcDateTime. PR #9.
0.0.5
- Exclude columns before checking their Python types. PR #5 by @ZachMyers3.
0.0.4
- Do not include SQLAlchemy defaults in Pydantic models. PR #4.
0.0.3
- Add support for
exclude
to exclude columns from Pydantic model. PR #3. - Add support for overriding the Pydantic
config
. PR #1 by @pyropy. - Add CI with GitHub Actions. PR #2.
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
Close
Hashes for pydantic-sqlalchemy-0.0.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 658362ea28dfaed2bd08605b0f6a4ff343a12f8fd1edb95919d9df557171059a |
|
MD5 | f080eb746da2c466d616fb731f6cc52d |
|
BLAKE2b-256 | 504a76120373e1d3a3e26a6ab6c1b158c322b945393e1906d275a06dc255ee56 |
Close
Hashes for pydantic_sqlalchemy-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4dd38bedd02040f3eb5ee360ad6396877eb674897c057a44e6460bb06e3c281 |
|
MD5 | b8efa17a1945c9026c8a77cca19b6d12 |
|
BLAKE2b-256 | 73f1ce4c8389248d90f6fe577b63c0b3963916b963ca8b0f75a043d96f712e7e |