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.2.tar.gz (3.5 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.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for partial_tables-0.0.2.tar.gz
Algorithm Hash digest
SHA256 12f139f84c374e5b63005cbd9cb7684d24441d8e770cc071783ccf4433837d3e
MD5 674c71d2a3e8fc09d53433786f7a97bc
BLAKE2b-256 cf7a61c8e7cda0d9b7b11f6959121c4392b3ccfc986558a8c37f02acc19e9b64

See more details on using hashes here.

Provenance

The following attestation bundles were made for partial_tables-0.0.2.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.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for partial_tables-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1d32a1e10d3ae7b6d907246c8d5e841aab8972936273e31dbffd78017300ac9
MD5 b95a1ee5bc5e6106bf8c32f0b4269e99
BLAKE2b-256 03eb8cc0e01bba4905f9018571241bb9fc94b45113181c37ad7b4af4433d8bed

See more details on using hashes here.

Provenance

The following attestation bundles were made for partial_tables-0.0.2-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