Tools to convert SQLAlchemy models to Pydantic models. For use with SQLAlchemy 2.0
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_2 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.2
- Versioning fix (sqlalchemy >= 2.0.0)
0.0.1
- Forked from tiangolo/pydantic-sqlalchemy
- Added support for SQLAlchemy 2.0
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 pydantic_sqlalchemy_2-0.0.2.tar.gz
.
File metadata
- Download URL: pydantic_sqlalchemy_2-0.0.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.4 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31896d5dbeaa125f844ac025f5d9d7dd614037c1b8021797b8caa8c421be6f9c |
|
MD5 | 5d3f80b1bb0a948de76479a194618da8 |
|
BLAKE2b-256 | ddd9b2d33558e41fb32a92381e722b3202e2e41750cb5b99757965ac6d4fd269 |
File details
Details for the file pydantic_sqlalchemy_2-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: pydantic_sqlalchemy_2-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.4 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a79e5600128ff45e13b74c1cef16785586f504a950043ac997265a8d2cfd61e |
|
MD5 | b9baf2a938790298678c8fbf244cb69b |
|
BLAKE2b-256 | 19bcb6588dbffd2a1d110d3edf9921632f1cae5c351d8ff8203f16832094b5b8 |