Tools to convert SQLAlchemy models to Pydantic models
Project description
Pydantic-SQLAlchemy
Tools to generate Pydantic models from SQLAlchemy models.
Still experimental.
🚨 WARNING: Use SQLModel instead 🚨
SQLModel is a library that solves the same problem as this one, but in a much better way, also solving several other problems at the same time.
This project was to solve some simple use cases, to generate dynamic Pydantic models from SQLAlchemy models. But the result cannot be used very well in code as it doesn't have all the autocompletion and inline errors that a Pydantic model would have.
This was a very simple implementation, SQLModel is a much better solution, much better design and work behind it.
For most of the cases where you would use pydantic-sqlalchemy, you should use SQLModel instead.
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.10
Breaking Changes
Docs
Internal
- 👷 Update Dependabot. PR #239 by @tiangolo.
- 👷 Update Dependabot. PR #238 by @tiangolo.
- Dependabot: remove pip-level label assignment from grouped updates. PR #234 by @Copilot.
- 👷 Upgrade actions/checkout from v5 to v6. PR #225 by @tiangolo.
- 👷 Upgrade
latest-changesGitHub Action and pinactions/checkout@v5. PR #224 by @tiangolo. - ⬆ Bump actions/labeler from 5 to 6. PR #208 by @dependabot[bot].
- ⬆ Bump actions/download-artifact from 4 to 6. PR #219 by @dependabot[bot].
- ⬆ Bump pypa/gh-action-pypi-publish from 1.12.4 to 1.13.0. PR #207 by @dependabot[bot].
- ⬆ Bump actions/checkout from 4 to 5. PR #202 by @dependabot[bot].
- ⬆ Bump tiangolo/latest-changes from 0.3.2 to 0.4.0. PR #198 by @dependabot[bot].
- ⬆ Bump typing-extensions from 4.6.1 to 4.13.2. PR #173 by @dependabot[bot].
- ⬆ Bump astral-sh/setup-uv from 5 to 6. PR #176 by @dependabot[bot].
- ⬆ Bump ruff from 0.9.8 to 0.11.13. PR #182 by @dependabot[bot].
- Enable CI for Python 3.9 - 3.13. af66239 by @tiangolo.
- ⬆ Bump ruff from 0.7.1 to 0.9.8. PR #156 by @dependabot[bot].
- ⬆ Bump astral-sh/setup-uv from 3 to 5. PR #144 by @dependabot[bot].
- ⬆ Bump pypa/gh-action-pypi-publish from 1.11.0 to 1.12.4. PR #151 by @dependabot[bot].
- 🔥 Drop support for Python 3.7. PR #154 by @tiangolo.
- ⬆ Bump tiangolo/latest-changes from 0.3.1 to 0.3.2. PR #133 by @dependabot[bot].
- ⬆ Bump pypa/gh-action-pypi-publish from 1.10.3 to 1.11.0. PR #127 by @dependabot[bot].
- ⬆ Bump ruff from 0.6.9 to 0.7.1. PR #126 by @dependabot[bot].
- ⬆ Update pytest requirement from <8.0.0,>=7.0.1 to >=7.0.1,<9.0.0. PR #104 by @dependabot[bot].
- 👷 Add labeler GitHub Action. PR #102 by @tiangolo.
- 🔧 Re-create Python project config, dependencies, and CI, just to make CI run. PR #101 by @tiangolo.
- ⬆ Bump actions/checkout from 2 to 4. PR #62 by @dependabot[bot].
- 👷 Update issue-manager.yml GitHub Action permissions. PR #78 by @tiangolo.
- 👷 Update
latest-changesGitHub Action. PR #79 by @tiangolo. - 🔧 Add GitHub templates for discussions and issues, and security policy. PR #76 by @alejsdev.
- 👷 Add dependabot. PR #60 by @tiangolo.
- 👷 Update latest-changes GitHub Action. PR #59 by @tiangolo.
0.0.9
0.0.8.post1
0.0.8
- ⬆️ Upgrade
importlib-metadatato 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-metadatato 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
excludeto 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
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 pydantic_sqlalchemy-0.0.10.tar.gz.
File metadata
- Download URL: pydantic_sqlalchemy-0.0.10.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f44afbe6e38d601d325a8f74d9b9a0d3922634570378701e16019afa62f48a3
|
|
| MD5 |
f2fca74305953f9ce34293382c0dc2cf
|
|
| BLAKE2b-256 |
f2fd5f5c5c86194090554fdf45aa3c8816e4ebe0af5e7897effb82fbc8cd4fde
|
Provenance
The following attestation bundles were made for pydantic_sqlalchemy-0.0.10.tar.gz:
Publisher:
publish.yml on tiangolo/pydantic-sqlalchemy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_sqlalchemy-0.0.10.tar.gz -
Subject digest:
4f44afbe6e38d601d325a8f74d9b9a0d3922634570378701e16019afa62f48a3 - Sigstore transparency entry: 1572603025
- Sigstore integration time:
-
Permalink:
tiangolo/pydantic-sqlalchemy@658ec5939baa8548e9fea00240da0022fb6e38ab -
Branch / Tag:
refs/tags/0.0.10 - Owner: https://github.com/tiangolo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@658ec5939baa8548e9fea00240da0022fb6e38ab -
Trigger Event:
release
-
Statement type:
File details
Details for the file pydantic_sqlalchemy-0.0.10-py3-none-any.whl.
File metadata
- Download URL: pydantic_sqlalchemy-0.0.10-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e06d898328a836180cb8ddcb02ef1d9e18020a1ce073dd44862b0a9a9eef8c7
|
|
| MD5 |
25d5517db62d780a7ad7c031ee40d739
|
|
| BLAKE2b-256 |
bfa120c5098ba0ef7f796e084b3ab86834ec50eff6eac43cc15112f18d6af54b
|
Provenance
The following attestation bundles were made for pydantic_sqlalchemy-0.0.10-py3-none-any.whl:
Publisher:
publish.yml on tiangolo/pydantic-sqlalchemy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_sqlalchemy-0.0.10-py3-none-any.whl -
Subject digest:
3e06d898328a836180cb8ddcb02ef1d9e18020a1ce073dd44862b0a9a9eef8c7 - Sigstore transparency entry: 1572603028
- Sigstore integration time:
-
Permalink:
tiangolo/pydantic-sqlalchemy@658ec5939baa8548e9fea00240da0022fb6e38ab -
Branch / Tag:
refs/tags/0.0.10 - Owner: https://github.com/tiangolo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@658ec5939baa8548e9fea00240da0022fb6e38ab -
Trigger Event:
release
-
Statement type: