Skip to main content

Partial Table Models for SQLAlchemy

Project description

SQLAlchemy Partial Tables

Partial Tables for SQLAlchemy

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.

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_tables-0.0.1a2.tar.gz (2.6 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.1a2-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file partial_tables-0.0.1a2.tar.gz.

File metadata

  • Download URL: partial_tables-0.0.1a2.tar.gz
  • Upload date:
  • Size: 2.6 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_tables-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 fd0375d1a87499009c8c48f261b2042021fc02089225f6dde20d333d4b91573d
MD5 b6c2bdb9d8a439538c28d64b820c5224
BLAKE2b-256 d9ba2955cdda78e71bde0eace2107bf5913507177d21d0eced8e9ffb26c450a3

See more details on using hashes here.

File details

Details for the file partial_tables-0.0.1a2-py3-none-any.whl.

File metadata

  • Download URL: partial_tables-0.0.1a2-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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_tables-0.0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 3ecf04173f115236721b86faf3b0d5312125ca398ecd69daeba0139a8ecac643
MD5 97ff1e87728c9483148564012e092353
BLAKE2b-256 c75b31db7632bb5339cf910433aef6566214ecb7d579619248b2bb03891cd960

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