Tools to convert SQLAlchemy models to Pydantic models
Project description
Tools to generate Pydantic models from SQLAlchemy models.
forked from tiangolo/pydantic-sqlalchemy
Only support pydantic V2
How to use
Quick example:
from typing import List
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.orm import Session, declarative_base, relationship, sessionmaker
from sqlalchemy_to_pydantic import sqlalchemy_to_pydantic
Base = declarative_base()
engine = create_engine("sqlite://")
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.model_validate(user)
data = pydantic_user.model_dump()
assert data == {
"fullname": "Ed Jones",
"id": 1,
"name": "ed",
"nickname": "edsnickname",
}
pydantic_user_with_addresses = PydanticUserWithAddresses.model_validate(user)
data = pydantic_user_with_addresses.model_dump()
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
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_to_pydantic-0.0.5.tar.gz
.
File metadata
- Download URL: sqlalchemy_to_pydantic-0.0.5.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-82-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8e3903778035ee95c7a1f49441e0ae7800e55502e81cbe8ea3bcb175f401b7b |
|
MD5 | 64c9847dfe22c8bb23ab21803544b231 |
|
BLAKE2b-256 | 2e8f940dcdf5e9fb47181c7d8e7623b5b017e45191b2e8f7aaa9f875e09b66fd |
File details
Details for the file sqlalchemy_to_pydantic-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_to_pydantic-0.0.5-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-82-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64353639d672ec010c571b907eb43a15219b0ae4cd5781c2c856aad5fc771a40 |
|
MD5 | b972431245ce568914b5ea03cf98509b |
|
BLAKE2b-256 | 82d19f66b2192bda4ce07406a9910e47c9796a707ea730ca091c077b112546dc |