Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tiangolo/pydantic-sqlalchemy
Tools to convert SQLAlchemy models to Pydantic models
https://github.com/tiangolo/pydantic-sqlalchemy
Last synced: 23 days ago
JSON representation
Tools to convert SQLAlchemy models to Pydantic models
- Host: GitHub
- URL: https://github.com/tiangolo/pydantic-sqlalchemy
- Owner: tiangolo
- License: mit
- Created: 2020-05-02T16:44:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-07T20:33:29.000Z (about 1 month ago)
- Last Synced: 2024-10-08T10:17:59.133Z (about 1 month ago)
- Language: Python
- Size: 101 KB
- Stars: 1,165
- Watchers: 20
- Forks: 69
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-fastapi - Pydantic-SQLAlchemy - Convert SQLAlchemy models to [Pydantic](https://pydantic-docs.helpmanual.io/) models. (Third-Party Extensions / Databases)
- awesome-fastapi - Pydantic-SQLAlchemy - Convert SQLAlchemy models to [Pydantic](https://docs.pydantic.dev/latest/) models. (Third-Party Extensions / Databases)
- jimsghstars - tiangolo/pydantic-sqlalchemy - Tools to convert SQLAlchemy models to Pydantic models (Python)
README
# Pydantic-SQLAlchemy
Tools to generate Pydantic models from SQLAlchemy models.
Still experimental.
## 🚨 WARNING: Use SQLModel instead 🚨
[SQLModel](https://sqlmodel.tiangolo.com/) 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](https://sqlmodel.tiangolo.com/) instead.
## How to use
Quick example:
```Python
from typing import Listfrom 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, sessionmakerBase = 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="[email protected]")
address2 = Address(email_address="[email protected]")
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": "[email protected]", "id": 1, "user_id": 1},
{"email_address": "[email protected]", "id": 2, "user_id": 1},
],
}
```## Release Notes
### Latest Changes
#### Docs
* 📝 Add SQLModel docs. PR [#70](https://github.com/tiangolo/pydantic-sqlalchemy/pull/70) by [@tiangolo](https://github.com/tiangolo).
#### Internal
* ⬆ Update pytest requirement from <8.0.0,>=7.0.1 to >=7.0.1,<9.0.0. PR [#104](https://github.com/tiangolo/pydantic-sqlalchemy/pull/104) by [@dependabot[bot]](https://github.com/apps/dependabot).
* 👷 Add labeler GitHub Action. PR [#102](https://github.com/tiangolo/pydantic-sqlalchemy/pull/102) by [@tiangolo](https://github.com/tiangolo).
* 🔧 Re-create Python project config, dependencies, and CI, just to make CI run. PR [#101](https://github.com/tiangolo/pydantic-sqlalchemy/pull/101) by [@tiangolo](https://github.com/tiangolo).
* ⬆ Bump actions/checkout from 2 to 4. PR [#62](https://github.com/tiangolo/pydantic-sqlalchemy/pull/62) by [@dependabot[bot]](https://github.com/apps/dependabot).
* 👷 Update issue-manager.yml GitHub Action permissions. PR [#78](https://github.com/tiangolo/pydantic-sqlalchemy/pull/78) by [@tiangolo](https://github.com/tiangolo).
* 👷 Update `latest-changes` GitHub Action. PR [#79](https://github.com/tiangolo/pydantic-sqlalchemy/pull/79) by [@tiangolo](https://github.com/tiangolo).
* 🔧 Add GitHub templates for discussions and issues, and security policy. PR [#76](https://github.com/tiangolo/pydantic-sqlalchemy/pull/76) by [@alejsdev](https://github.com/alejsdev).
* 👷 Add dependabot. PR [#60](https://github.com/tiangolo/pydantic-sqlalchemy/pull/60) by [@tiangolo](https://github.com/tiangolo).
* 👷 Update latest-changes GitHub Action. PR [#59](https://github.com/tiangolo/pydantic-sqlalchemy/pull/59) by [@tiangolo](https://github.com/tiangolo).### 0.0.9
* ✨ Add `poetry-version-plugin`, remove `importlib-metadata` dependency. PR [#32](https://github.com/tiangolo/pydantic-sqlalchemy/pull/32) by [@tiangolo](https://github.com/tiangolo).
### 0.0.8.post1
* 💚 Fix setting up Poetry for GitHub Action Publish. PR [#23](https://github.com/tiangolo/pydantic-sqlalchemy/pull/23) by [@tiangolo](https://github.com/tiangolo).
### 0.0.8
* ⬆️ Upgrade `importlib-metadata` to 3.0.0. PR [#22](https://github.com/tiangolo/pydantic-sqlalchemy/pull/22) by [@tiangolo](https://github.com/tiangolo).
* 👷 Add GitHub Action latest-changes. PR [#20](https://github.com/tiangolo/pydantic-sqlalchemy/pull/20) by [@tiangolo](https://github.com/tiangolo).
* 💚 Fix GitHub Actions Poetry setup. PR [#21](https://github.com/tiangolo/pydantic-sqlalchemy/pull/21) by [@tiangolo](https://github.com/tiangolo).### 0.0.7
* Update requirements of `importlib-metadata` to support the latest version `2.0.0`. PR [#11](https://github.com/tiangolo/pydantic-sqlalchemy/pull/11).
### 0.0.6
* Add support for SQLAlchemy extended types like [sqlalchemy-utc: UtcDateTime](https://github.com/spoqa/sqlalchemy-utc). PR [#9](https://github.com/tiangolo/pydantic-sqlalchemy/pull/9).
### 0.0.5
* Exclude columns before checking their Python types. PR [#5](https://github.com/tiangolo/pydantic-sqlalchemy/pull/5) by [@ZachMyers3](https://github.com/ZachMyers3).
### 0.0.4
* Do not include SQLAlchemy defaults in Pydantic models. PR [#4](https://github.com/tiangolo/pydantic-sqlalchemy/pull/4).
### 0.0.3
* Add support for `exclude` to exclude columns from Pydantic model. PR [#3](https://github.com/tiangolo/pydantic-sqlalchemy/pull/3).
* Add support for overriding the Pydantic `config`. PR [#1](https://github.com/tiangolo/pydantic-sqlalchemy/pull/1) by [@pyropy](https://github.com/pyropy).
* Add CI with GitHub Actions. PR [#2](https://github.com/tiangolo/pydantic-sqlalchemy/pull/2).## License
This project is licensed under the terms of the MIT license.