Skip to main content

sqlalchemy-pydantic-orm

This library makes it a lot easier to do nested database operation with SQLAlchemy. With this library it is for example possible to validate, convert, and upload a 100-level deep nested JSON (dict) to its corresponding tables in a given database, within 3 lines of code.

Pydantic is used for creating the dataclass and validating it. Pydantic already has a function called .from_orm() that can do a nested get operation, but it only supports ORM -> Pydantic and not Pydantic -> ORM. That's exactly where this library fills in, with 2 specific functions .orm_create() and .orm_update(), and one general function .to_orm() that combines the functionality of the first 2, calling one or the other, depending on if there is an id provided.

Requirements

  • Python 3.8+
  • SQLAlchemy 1.4+
  • Pydantic 1.8+

Installation

$ pip install sqlalchemy-pydantic-orm

To tinker with the code yourself, install the full dependencies with:

$ pip install sqlalchemy-pydantic-orm[dev]

Useful references

Examples

Below 1 example is provided (more coming).

For a bigger and more detailed example you can look at the /examples/ folder.

Example 1 - Using manual created schemas

Create your own Pydantic schemas and link them to the SQLAlchemy ORM-models.

Create your SQLAlchemy ORM-models (one-to-one or one-to-many)

class Parent(Base):
    id = Column(Integer, primary_key=True, index=True, nullable=False)
    name = Column(String, nullable=False)
    car = relationship("Car", cascade="all, delete", uselist=False, back_populates="owner")
    children = relationship("Child", cascade="all, delete")

class Car(Base):
    id = Column(Integer, primary_key=True, index=True, nullable=False)
    color = Column(String, nullable=False)
    owner_id = Column(Integer, ForeignKey("parents.id"), nullable=False)
    owner = relationship("Parent", back_populates="car")

class Child(Base):
    id = Column(Integer, primary_key=True, index=True, nullable=False)
    name = Column(String, nullable=False)
    parent_id = Column(Integer, ForeignKey("parents.id"), nullable=False)

Create your Pydantic base and CRUD schemas using these ORM models, and the imported ORMBaseSchema

Base schemas

from sqlalchemy_pydantic_orm import ORMBaseSchema
from .models import Parent, Car, Child

class ParentBase(ORMBaseSchema):
    name: str
    _orm_model = PrivateAttr(Parent)

class CarBase(ORMBaseSchema):
    color: str
    _orm_model = PrivateAttr(models.Car)

class ChildBase(ORMBaseSchema):
    name: str
    _orm_model = PrivateAttr(models.Child)

GET schemas

class Parent(ParentBase):
    id: int
    children: List[Child]
    car: Car

class Car(CarBase):
    id: int

class Child(ChildBase):
    id: int

CREATE/UPDATE schemas

class ParentCreate(ParentBase):
    id: Optional[int]
    children: List[ChildCreate]
    car: CarCreate

class CarCreate(CarBase):
    id: Optional[int]

class ChildCreate(ChildBase):
    id: Optional[int]

Use your schemas to do nested CRUD operations.

with ConnectionDatabase() as db:
    create_schema = schemas.ParentCreate.parse_obj(create_dict)
    parent_db = create_schema.orm_create()
    db.add(parent_db)
    db.commit()

    db_create_schema = schemas.Parent.from_orm(parent_db)
    print(db_create_schema.dict())

    update_schema = schemas.ParentUpdate.parse_obj(update_dict)
    update_schema.to_orm(db)
    db.commit()

    db_update_schema = schemas.Parent.orm_update(parent_db)
    print(db_update_schema.dict())

Note: with .orm_create() you have to call db.add() before calling db.commit(). With orm_update you give the db session as parameter, and you only have to call db.commit().

Example 2 - Using generated schemas

TODO: Integrate with https://github.com/tiangolo/pydantic-sqlalchemy

Release files for sqlalchemy-pydantic-orm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlalchemy-pydantic-orm 0.1.0
File Size Uploaded
sqlalchemy-pydantic-orm-0.1.0.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-pydantic-orm 0.1.0
File Interpreter ABI Platform
sqlalchemy_pydantic_orm-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.3 kB

Release files / sqlalchemy-pydantic-orm-0.1.0.tar.gz

Download URL sqlalchemy-pydantic-orm-0.1.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0b15bc38eebedf92af044e12707dad7ae19b2eaf6318fbc5a8d64ec3a3f35e1d
BLAKE2b-256 checksum
How to use checksums
39292e623cd5620508169abc65b7e188260d0ed6fada8480f89d7fc0ded6d682
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.3

Release files / sqlalchemy_pydantic_orm-0.1.0-py3-none-any.whl

Download URL sqlalchemy_pydantic_orm-0.1.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0aa423514424964ae89f635089f2f8173ffe804d9f54c120e23ed54c116cef10
BLAKE2b-256 checksum
How to use checksums
9be3a589ad5b48d1a2b2775845b1c3f084b30869a444eb2d08b8f30fc1929bb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page