Skip to main content

Partial Table Models for SQLAlchemy and SQLModel

Project description

SQLAlchemy Partial Tables

Partial Tables for SQLAlchemy and SQLModel

Coverage

Installation

pip install partial-tables

Scenario

Let's say you have 2 tables, business_draft and business.

business_draft and business have the same fields, but business_draft should allow most fields to be nullable.

Any business can freely update its draft, but only approved modifications get copied over to business.

How can we implement this and reduce redundancy?

Usage

Any field marked with PartialAllowed will be nullable in the partial table, and required in the complete table.

A partial table is any table that sub-classes with PartialTable.

Example (SQLAlchemy Declarative)

from typing import Annotated
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from partial_tables import PartialSQLAlchemyMixin, PartialAllowed, PartialTable


class Base(DeclarativeBase):
    __abstract__ = True


class BusinessBase(PartialSQLAlchemyMixin, Base):
    """Base class for all business models."""

    __abstract__ = True

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    business_name: Mapped[str] = mapped_column()
    # Mark fields that may be nullable in the partial table
    city: Mapped[Annotated[str, PartialAllowed()]] = mapped_column()
    address: Mapped[Annotated[str, PartialAllowed()]] = mapped_column()


class BusinessDraft(BusinessBase, PartialTable):
    __tablename__ = "business_draft"


class Business(BusinessBase):
    __tablename__ = "business"

Business has all required (NOT NULL) columns, and BusinessDraft has every field marked with PartialAllowed as nullable.

Example (SQLModel)

from typing import Annotated
from sqlmodel import SQLModel, Field
from partial_tables import PartialSQLModelMixin, PartialAllowed, PartialTable


class BusinessBase(PartialSQLModelMixin, SQLModel):
    id: int = Field(primary_key=True, sa_column_kwargs={"autoincrement": True})
    business_name: str
    city: Annotated[str, PartialAllowed()] = Field()
    address: Annotated[str, PartialAllowed()] = Field()


class BusinessDraft(BusinessBase, PartialTable, table=True):
    __tablename__ = "business_draft"


class Business(BusinessBase, table=True):
    __tablename__ = "business"

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

partial_tables-0.0.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

partial_tables-0.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file partial_tables-0.0.3.tar.gz.

File metadata

  • Download URL: partial_tables-0.0.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for partial_tables-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7d14924708a925f9c842451681bbd455b87987826142a4d75ad20649d5df6bc4
MD5 84525cc27ae57057a22fa970fdfdbe63
BLAKE2b-256 0287dd5ec71b534c4f744a2f24c6215e577ff8b270a6119106c9873e91191444

See more details on using hashes here.

Provenance

The following attestation bundles were made for partial_tables-0.0.3.tar.gz:

Publisher: publish-to-pypi.yml on julien777z/partial-tables

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file partial_tables-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: partial_tables-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for partial_tables-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 127db3e2db4d2da6ee35733a54360205352e479cea36c66532087d51b2a12d2c
MD5 ea6e52b9d155715b3305439b7c56dcfb
BLAKE2b-256 558058e1f763de1d7cee393afaf4c7a34a5f7d87bce6b3987e5dcbe7598a172b

See more details on using hashes here.

Provenance

The following attestation bundles were made for partial_tables-0.0.3-py3-none-any.whl:

Publisher: publish-to-pypi.yml on julien777z/partial-tables

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page