Partial Table Models for SQLAlchemy and SQLModel
Project description
SQLAlchemy Partial Tables
Partial Tables for SQLAlchemy and SQLModel
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12f139f84c374e5b63005cbd9cb7684d24441d8e770cc071783ccf4433837d3e
|
|
| MD5 |
674c71d2a3e8fc09d53433786f7a97bc
|
|
| BLAKE2b-256 |
cf7a61c8e7cda0d9b7b11f6959121c4392b3ccfc986558a8c37f02acc19e9b64
|
Provenance
The following attestation bundles were made for partial_tables-0.0.2.tar.gz:
Publisher:
publish-to-pypi.yml on julien777z/partial-tables
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
partial_tables-0.0.2.tar.gz -
Subject digest:
12f139f84c374e5b63005cbd9cb7684d24441d8e770cc071783ccf4433837d3e - Sigstore transparency entry: 684000686
- Sigstore integration time:
-
Permalink:
julien777z/partial-tables@81cf65241f350b5edeb9dc64aa018670612277b0 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@81cf65241f350b5edeb9dc64aa018670612277b0 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1d32a1e10d3ae7b6d907246c8d5e841aab8972936273e31dbffd78017300ac9
|
|
| MD5 |
b95a1ee5bc5e6106bf8c32f0b4269e99
|
|
| BLAKE2b-256 |
03eb8cc0e01bba4905f9018571241bb9fc94b45113181c37ad7b4af4433d8bed
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
partial_tables-0.0.2-py3-none-any.whl -
Subject digest:
d1d32a1e10d3ae7b6d907246c8d5e841aab8972936273e31dbffd78017300ac9 - Sigstore transparency entry: 684000752
- Sigstore integration time:
-
Permalink:
julien777z/partial-tables@81cf65241f350b5edeb9dc64aa018670612277b0 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@81cf65241f350b5edeb9dc64aa018670612277b0 -
Trigger Event:
workflow_dispatch
-
Statement type: