pydantic-sqlalchemy-2


Namepydantic-sqlalchemy-2 JSON
Version 0.0.2 PyPI version JSON
download
home_page
SummaryTools to convert SQLAlchemy models to Pydantic models. For use with SQLAlchemy 2.0
upload_time2023-10-12 17:17:49
maintainer
docs_urlNone
authorEric Richard
requires_python>=3.7,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pydantic-SQLAlchemy

<a href="https://github.com/tiangolo/pydantic-sqlalchemy/actions?query=workflow%3ATest" target="_blank">
    <img src="https://github.com/tiangolo/pydantic-sqlalchemy/workflows/Test/badge.svg" alt="Test">
</a>
<a href="https://codecov.io/gh/tiangolo/pydantic-sqlalchemy" target="_blank">
    <img src="https://img.shields.io/codecov/c/github/tiangolo/pydantic-sqlalchemy?color=%2334D058" alt="Coverage">
</a>
<a href="https://pypi.org/project/pydantic-sqlalchemy" target="_blank">
    <img src="https://img.shields.io/pypi/v/pydantic-sqlalchemy-2?color=%2334D058&label=pypi%20package" alt="Package version">
</a>

Tools to generate Pydantic models from SQLAlchemy models.

Still experimental.

## How to use

Quick example:

```Python
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](https://github.com/tiangolo/pydantic-sqlalchemy/fork)
* Added support for SQLAlchemy 2.0

## License

This project is licensed under the terms of the MIT license.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pydantic-sqlalchemy-2",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Eric Richard",
    "author_email": "ehutzle@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/dd/d9/b2d33558e41fb32a92381e722b3202e2e41750cb5b99757965ac6d4fd269/pydantic_sqlalchemy_2-0.0.2.tar.gz",
    "platform": null,
    "description": "# Pydantic-SQLAlchemy\n\n<a href=\"https://github.com/tiangolo/pydantic-sqlalchemy/actions?query=workflow%3ATest\" target=\"_blank\">\n    <img src=\"https://github.com/tiangolo/pydantic-sqlalchemy/workflows/Test/badge.svg\" alt=\"Test\">\n</a>\n<a href=\"https://codecov.io/gh/tiangolo/pydantic-sqlalchemy\" target=\"_blank\">\n    <img src=\"https://img.shields.io/codecov/c/github/tiangolo/pydantic-sqlalchemy?color=%2334D058\" alt=\"Coverage\">\n</a>\n<a href=\"https://pypi.org/project/pydantic-sqlalchemy\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/v/pydantic-sqlalchemy-2?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n</a>\n\nTools to generate Pydantic models from SQLAlchemy models.\n\nStill experimental.\n\n## How to use\n\nQuick example:\n\n```Python\nfrom typing import List\n\nfrom pydantic_sqlalchemy_2 import sqlalchemy_to_pydantic\nfrom sqlalchemy import Column, ForeignKey, Integer, String, create_engine\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy.orm import Session, relationship, sessionmaker\n\nBase = declarative_base()\n\nengine = create_engine(\"sqlite://\", echo=True)\n\n\nclass User(Base):\n    __tablename__ = \"users\"\n\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n    fullname = Column(String)\n    nickname = Column(String)\n\n    addresses = relationship(\n        \"Address\", back_populates=\"user\", cascade=\"all, delete, delete-orphan\"\n    )\n\n\nclass Address(Base):\n    __tablename__ = \"addresses\"\n    id = Column(Integer, primary_key=True)\n    email_address = Column(String, nullable=False)\n    user_id = Column(Integer, ForeignKey(\"users.id\"))\n\n    user = relationship(\"User\", back_populates=\"addresses\")\n\n\nPydanticUser = sqlalchemy_to_pydantic(User)\nPydanticAddress = sqlalchemy_to_pydantic(Address)\n\n\nclass PydanticUserWithAddresses(PydanticUser):\n    addresses: List[PydanticAddress] = []\n\n\nBase.metadata.create_all(engine)\n\nLocalSession = sessionmaker(bind=engine)\n\ndb: Session = LocalSession()\n\ned_user = User(name=\"ed\", fullname=\"Ed Jones\", nickname=\"edsnickname\")\n\naddress = Address(email_address=\"ed@example.com\")\naddress2 = Address(email_address=\"eddy@example.com\")\ned_user.addresses = [address, address2]\ndb.add(ed_user)\ndb.commit()\n\n\ndef test_pydantic_sqlalchemy():\n    user = db.query(User).first()\n    pydantic_user = PydanticUser.from_orm(user)\n    data = pydantic_user.dict()\n    assert data == {\n        \"fullname\": \"Ed Jones\",\n        \"id\": 1,\n        \"name\": \"ed\",\n        \"nickname\": \"edsnickname\",\n    }\n    pydantic_user_with_addresses = PydanticUserWithAddresses.from_orm(user)\n    data = pydantic_user_with_addresses.dict()\n    assert data == {\n        \"fullname\": \"Ed Jones\",\n        \"id\": 1,\n        \"name\": \"ed\",\n        \"nickname\": \"edsnickname\",\n        \"addresses\": [\n            {\"email_address\": \"ed@example.com\", \"id\": 1, \"user_id\": 1},\n            {\"email_address\": \"eddy@example.com\", \"id\": 2, \"user_id\": 1},\n        ],\n    }\n```\n\n## Release Notes\n\n### Latest Changes\n\n\n### 0.0.2\n\n* Versioning fix (sqlalchemy >= 2.0.0)\n\n### 0.0.1\n\n* Forked from [tiangolo/pydantic-sqlalchemy](https://github.com/tiangolo/pydantic-sqlalchemy/fork)\n* Added support for SQLAlchemy 2.0\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Tools to convert SQLAlchemy models to Pydantic models. For use with SQLAlchemy 2.0",
    "version": "0.0.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "19bcb6588dbffd2a1d110d3edf9921632f1cae5c351d8ff8203f16832094b5b8",
                "md5": "b9baf2a938790298678c8fbf244cb69b",
                "sha256": "5a79e5600128ff45e13b74c1cef16785586f504a950043ac997265a8d2cfd61e"
            },
            "downloads": -1,
            "filename": "pydantic_sqlalchemy_2-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b9baf2a938790298678c8fbf244cb69b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 4065,
            "upload_time": "2023-10-12T17:17:48",
            "upload_time_iso_8601": "2023-10-12T17:17:48.259587Z",
            "url": "https://files.pythonhosted.org/packages/19/bc/b6588dbffd2a1d110d3edf9921632f1cae5c351d8ff8203f16832094b5b8/pydantic_sqlalchemy_2-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ddd9b2d33558e41fb32a92381e722b3202e2e41750cb5b99757965ac6d4fd269",
                "md5": "5d3f80b1bb0a948de76479a194618da8",
                "sha256": "31896d5dbeaa125f844ac025f5d9d7dd614037c1b8021797b8caa8c421be6f9c"
            },
            "downloads": -1,
            "filename": "pydantic_sqlalchemy_2-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "5d3f80b1bb0a948de76479a194618da8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 3516,
            "upload_time": "2023-10-12T17:17:49",
            "upload_time_iso_8601": "2023-10-12T17:17:49.927025Z",
            "url": "https://files.pythonhosted.org/packages/dd/d9/b2d33558e41fb32a92381e722b3202e2e41750cb5b99757965ac6d4fd269/pydantic_sqlalchemy_2-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-12 17:17:49",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pydantic-sqlalchemy-2"
}
        
Elapsed time: 0.30985s