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.
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)
make install- install the required dependencies.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:
make lint- check for linting issues.make format- fix linting issues.
CICD
Publishing to PyPI
We publish to PyPI using GitHub releases and PyPI trusted publishing. Steps are as follows:
- Manually update the version in
pyproject.tomlfile using a PR and merge to main. Useuv version --bump {patch/minor/major}to update the version. - Create a new release in GitHub with the tag name as the version number. This will trigger the
publishworkflow. In the Release window, type in the version number and it will prompt to create a new tag. - 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
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 sqlalchemy_persisted_hybrid_property-1.0.3.tar.gz.
File metadata
- Download URL: sqlalchemy_persisted_hybrid_property-1.0.3.tar.gz
- Upload date:
- Size: 15.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60a63cf61e5b12457a7c8d3073921536d83d1caf088664fa2569b574ef035369
|
|
| MD5 |
c9920e6d8e9a01471d6d382ea48f5c1d
|
|
| BLAKE2b-256 |
f41198e1a28e8ae0be89f64bddb3d05408fa7464005995a43baea095eb4c95e2
|
File details
Details for the file sqlalchemy_persisted_hybrid_property-1.0.3-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_persisted_hybrid_property-1.0.3-py3-none-any.whl
- Upload date:
- Size: 17.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ec5b759dc0e2f017238f46ac0aa0ea5b7c654dba2290af34f8a3bc1bcb2ecc4
|
|
| MD5 |
a18f057627346b9ef9fbe7d53c90b5e0
|
|
| BLAKE2b-256 |
d2458f00cea76db60a2bdc2d5f44459ac2b133cf69e0ee46584a4d03b572ffb7
|