Skip to main content

SQLAlchemy Persisted Hybrid Property

Persist selected SQLAlchemy hybrid_property values into real database columns, while keeping normal hybrid descriptor behavior for Python access and SQL queries.

This is useful when a value is derived from ORM state, should still be queryable as a hybrid expression, but also needs to exist physically in the table for reads, migrations, reporting, indexing, or compatibility with code that expects a stored column.

Python UV Hatchling Ruff Pre-commit Pytest Coverage GitHub Actions PyPI Makefile

CI


Table of Contents


Introduction

This template repository aims to streamline the creation, testing, and publishing of isolated Python packages.


Quick Start

Since this is just a package, and not a service, there is no real "run" action. But you can run the tests immediately.

Here are a list of available commands via make.

Bare Metal (i.e. your machine)

  1. make install - install the required dependencies.
  2. make test - runs the tests.

Installation

For Dev work on the repo

Install uv, (if you haven't already) https://docs.astral.sh/uv/getting-started/installation/#installation-methods

brew install uv

Initialise pre-commit (validates ruff on commit.)

uv run pre-commit install

Install dependencies (including dev dependencies)

uv sync

If you are adding a new dev dependency, please run:

uv add --dev {your-new-package}

Quick Example

from sqlalchemy import ForeignKey, Integer, func, select
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship

from sqlalchemy_persisted_hybrid_property import hybrid_property_persisted


class Base(DeclarativeBase):
    pass


class Quote(Base):
    __tablename__ = "quote"

    id: Mapped[int] = mapped_column(primary_key=True)
    line_items: Mapped[list["LineItem"]] = relationship(back_populates="quote")

    @hybrid_property_persisted(
        type_=Integer,
        nullable=False,
        default=0,
        materialize="auto",
    )
    def line_item_count(self) -> int:
        return len(self.line_items)

    @line_item_count.inplace.expression
    @classmethod
    def _line_item_count_expression(cls):
        return (
            select(func.count(LineItem.id))
            .where(LineItem.quote_id == cls.id)
            .correlate(cls)
            .scalar_subquery()
        )


class LineItem(Base):
    __tablename__ = "line_item"

    id: Mapped[int] = mapped_column(primary_key=True)
    quote_id: Mapped[int] = mapped_column(ForeignKey("quote.id"))
    quote: Mapped[Quote] = relationship(back_populates="line_items")

The mapped table receives a real column:

line_item_count INTEGER NOT NULL

Then normal ORM work is enough:

quote.line_items.append(LineItem())
session.add(quote)
session.flush()

The package updates the hidden backing column during the flush lifecycle. You keep using quote.line_item_count as a hybrid property.

API

@hybrid_property_persisted(
    type_=None,
    column_name=None,
    nullable=None,
    default=None,
    server_default=None,
    depends_on="auto",
    materialize="auto",
)
def value(self) -> int:
    ...

type_ may be a SQLAlchemy type instance or class. If omitted, common return annotations are inferred: int, bool, float, str, date, datetime, Decimal, UUID, enums, and nullable unions like int | None.

column_name defaults to the property name.

nullable defaults from the return annotation for Python materialization. For SQL materialization, nullable storage is allowed so rows with database-generated primary keys can be inserted before the post-flush SQL update runs.

depends_on controls invalidation:

depends_on="auto"
depends_on="value"
depends_on=["children", "children.value"]

auto infers dependencies from the SQL expression when available, and otherwise from unambiguous mapper relationship paths. Ambiguous paths fail loudly; use explicit paths in that case.

materialize controls how values are written:

materialize="auto"    # prefer SQL expression, fall back to Python getter
materialize="sql"     # require a hybrid SQL expression
materialize="python"  # evaluate the Python getter on the owner object

SQLModel

Use the SQLModel shim if you want persisted hybrids in SQLModel classes:

from sqlmodel import Field

from sqlalchemy_persisted_hybrid_property import hybrid_property_persisted
from sqlalchemy_persisted_hybrid_property.sqlmodel import SQLModel


class Metric(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    value: int

    @hybrid_property_persisted()
    def doubled(self) -> int:
        return self.value * 2

The hidden backing attribute is ignored by Pydantic/SQLModel fields and does not appear in model_dump().

How It Works

The package is SQLAlchemy-native. It subclasses SQLAlchemy's hybrid_property instead of wrapping or cloning it, so .expression, .setter, .update_expression, custom comparators, and .inplace modifiers keep normal hybrid semantics.

During mapper construction, persisted hybrid descriptors are discovered via SQLAlchemy mapper descriptors. A physical Column is injected into the mapped table and mapped under a hidden storage key such as __php_line_item_count, leaving the public hybrid name untouched.

During flush, changed ORM instances are resolved back to affected owner instances using SQLAlchemy inspection, relationship metadata, and attribute history. Relationship removals, deletes, reparenting, unloaded FK changes, many-to-many collections, composite primary keys, and async sessions are supported by the test suite.

When a SQL expression is available, materialization uses batched SQL like:

UPDATE quote
SET line_item_count = (
    SELECT count(...)
)
WHERE quote.id IN (...)

This avoids calling the Python getter for SQL-capable properties and keeps inserts/deletes visible by running after ORM DML has reached the database. Python materialization writes the hidden mapped attribute directly, so SQLAlchemy naturally persists the stored value.

Alembic

Injected columns are added to Base.metadata, so Alembic autogenerate can see them as long as your models and this package are imported before autogenerate runs.


Formatting and linting

We use Ruff as the formatter and linter. The pre-commit has hooks which runs checking and applies linting automatically. The CI validates the linting, ensuring main is always looking clean.

You can manually use these commands too:

  1. make lint - check for linting issues.
  2. make format - fix linting issues.

CICD

Publishing to PyPI

We publish to PyPI using GitHub releases and PyPI trusted publishing. Steps are as follows:

  1. Manually update the version in pyproject.toml file using a PR and merge to main. Use uv version --bump {patch/minor/major} to update the version.
  2. Create a new release in GitHub with the tag name as the version number. This will trigger the publish workflow. In the Release window, type in the version number and it will prompt to create a new tag.
  3. Verify the release on PyPI.

Credits

This template repository has taken inspiration from the following repositories.

Download files

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

Source Distribution

sqlalchemy_persisted_hybrid_property-1.0.1.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file sqlalchemy_persisted_hybrid_property-1.0.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_persisted_hybrid_property-1.0.1.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sqlalchemy_persisted_hybrid_property-1.0.1.tar.gz
Algorithm Hash digest
SHA256 071610c03af7eb088edd4377561712bf19e818a55b41ca613760081eadc846cf
MD5 5aa9476df036cee29184e9c6e4d13f6a
BLAKE2b-256 8534592d91780b46ece8bf2a2598d9719e83c57d7d3a0bf6177ade5918777e51

See more details on using hashes here.

File details

Details for the file sqlalchemy_persisted_hybrid_property-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_persisted_hybrid_property-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sqlalchemy_persisted_hybrid_property-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e481c7430733704eb427fdd815b7c8367280690f03588790a54c839ed42a792
MD5 43398fb045ffb71d7c65761f83657339
BLAKE2b-256 f184f6e2c946bf8a7357ecc7ab6d1e7621c6cadc0f0be61471545504fbc4cdfa

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.3

2 files

1.0.2

2 files

This release

1.0.1 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page