MyGit

tiangolo/pydantic-sqlalchemy

Fork: 71 Star: 1175 (更新于 2024-11-04 18:46:53)

license: MIT

Language: Python .

Tools to convert SQLAlchemy models to Pydantic models

最后发布版本: 0.0.9 ( 2021-05-30 17:38:06)

GitHub网址

Pydantic-SQLAlchemy

Test Publish Coverage Package version

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

Docs

Internal

0.0.9

  • ✨ Add poetry-version-plugin, remove importlib-metadata dependency. PR #32 by @tiangolo.

0.0.8.post1

  • 💚 Fix setting up Poetry for GitHub Action Publish. PR #23 by @tiangolo.

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 version 2.0.0. PR #11.

0.0.6

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.

最近版本更新:(数据更新于 2024-09-18 00:34:11)

2021-05-30 17:38:06 0.0.9

2020-11-24 04:15:42 0.0.8.post1

2020-11-24 03:49:56 0.0.8

2020-09-25 01:47:26 0.0.7

2020-08-31 22:57:43 0.0.6

2020-08-16 23:33:07 0.0.5

2020-05-08 00:08:30 0.0.4

2020-05-05 19:06:36 0.0.3

tiangolo/pydantic-sqlalchemy同语言 Python最近更新仓库

2024-11-22 19:26:55 ultralytics/ultralytics

2024-11-22 08:12:43 jxxghp/MoviePilot

2024-11-22 06:12:44 dagster-io/dagster

2024-11-22 02:39:01 goauthentik/authentik

2024-11-22 00:15:39 jumpserver/jumpserver

2024-11-22 00:03:47 comfyanonymous/ComfyUI