Skip to main content

Partial Table Models for SQLAlchemy

Project description

SQLAlchemy Partial Tables

Partial Tables for SQLAlchemy

Installation

pip install partial-table

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.

Example

from typing import Annotated
from abc import ABC
from sqlmodel import Field, SQLModel
from partial_table import PartialBase, PartialAllowed, PartialTable

class Base(ABC, SQLModel):
    """Base class for all models."""

    id: int = Field(primary_key=True, sa_column_kwargs={"autoincrement": True})


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

    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"

Business has all required fields, and BusinessDraft has every field marked with PartialAllowed as nullable.

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_table-0.0.1a0.tar.gz (2.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_table-0.0.1a0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file partial_table-0.0.1a0.tar.gz.

File metadata

  • Download URL: partial_table-0.0.1a0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.11 Darwin/23.5.0

File hashes

Hashes for partial_table-0.0.1a0.tar.gz
Algorithm Hash digest
SHA256 d747f832934bcd6f20ba05cb42cb715024c363e94e314bff2c1fbf3d35fdc9de
MD5 c373620451da92931648e835bfbfbfdc
BLAKE2b-256 f71548dcf19b1bd86a76139b5a2dd970675513347524e4057336e3a7bc501bdb

See more details on using hashes here.

File details

Details for the file partial_table-0.0.1a0-py3-none-any.whl.

File metadata

  • Download URL: partial_table-0.0.1a0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.10.11 Darwin/23.5.0

File hashes

Hashes for partial_table-0.0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5943b349a468de8e07fc384eb8a475c8b34f218e82e6288038302fdd8e5bf0b
MD5 bcce5503f5fa63bd343bde0f7e8d0a83
BLAKE2b-256 2625c1ad4fe23cb4aedd1adc3b4be35aa7c928cebe765642e2c7d5c941a29c56

See more details on using hashes here.

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