Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sqlalchemy_history/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,12 @@ def create_hybrid_properties(self, version_classes):
setattr(
versioned_target_class,
key,
sa.ext.hybrid.hybrid_property(fget=prop.fget),
sa.ext.hybrid.hybrid_property(
fget=prop.fget,
expr=prop.expr,
fset=prop.fset,
fdel=prop.fdel,
custom_comparator=prop.custom_comparator,
update_expr=prop.update_expr,
),
)
24 changes: 24 additions & 0 deletions tests/test_hybrid_property.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import sqlalchemy as sa
from sqlalchemy.orm import relationship
from sqlalchemy_history.utils import version_class

from tests import TestCase
Expand All @@ -17,11 +18,34 @@ class Article(self.Model):
description = sa.Column(sa.UnicodeText)
publish = sa.Column(sa.DateTime, default=lambda: datetime.datetime.now(datetime.timezone.utc))

author_id = sa.Column(sa.Integer, sa.ForeignKey("article_author.id"), nullable=False)

@sa.ext.hybrid.hybrid_property
def time_from_publish(self):
return datetime.datetime.today() - self.publish

@sa.ext.hybrid.hybrid_property
def author_name(self):
return self.author.name

@author_name.expression
def author_name(cls):
return (
sa.select(ArticleAuthor.name).where(ArticleAuthor.id == cls.author_id).scalar_subquery()
)

class ArticleAuthor(self.Model):
__tablename__ = "article_author"
__versioned__ = {}

id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
name = sa.Column(sa.Unicode(255), nullable=False)
articles = relationship("Article", backref="author")

self.Article = Article

def test_hybrid_property_mapping_for_versioned_class(self):
version_class(self.Article).time_from_publish

def test_version_class_hybrid_property_in_sql_expression(self):
sa.select(version_class(self.Article).author_name)