Hybrid Attributes
“hybrid” means the attribute has distinct behaviors defined at the class level and at the instance level.
The hybrid extension provides a special form of method decorator, is around 50 lines of code and has almost no dependencies on the rest of SQLAlchemy. It can, in theory, work with any descriptor-based expression system.
Consider a mapping , representing integer start
and end
values. We can define higher level functions on mapped classes that produce SQL expressions at the class level, and Python expression evaluation at the instance level. Below, each function decorated with or hybrid_property may receive self
as an instance of the class, or as the class itself:
Above, the length
property returns the difference between the end
and start
attributes. With an instance of Interval
, this subtraction occurs in Python, using normal Python descriptor mechanics:
>>> i1 = Interval(5, 10)
>>> i1.length
5
When dealing with the Interval
class itself, the descriptor evaluates the function body given the Interval
class as the argument, which when evaluated with SQLAlchemy expression mechanics (here using the QueryableAttribute.expression accessor) returns a new SQL expression:
>>> print(Interval.length.expression)
interval."end" - interval.start
>>> print(Session().query(Interval).filter(Interval.length > 10))
SELECT interval.id AS interval_id, interval.start AS interval_start,
interval."end" AS interval_end
FROM interval
WHERE interval."end" - interval.start > :param_1
ORM methods such as generally use getattr()
to locate attributes, so can also be used with hybrid attributes:
>>> print(Session().query(Interval).filter_by(length=5))
SELECT interval.id AS interval_id, interval.start AS interval_start,
interval."end" AS interval_end
FROM interval
WHERE interval."end" - interval.start = :param_1
The Interval
class example also illustrates two methods, contains()
and intersects()
, decorated with hybrid_method. This decorator applies the same idea to methods that applies to attributes. The methods return boolean values, and take advantage of the Python |
and &
bitwise operators to produce equivalent instance-level and SQL expression-level boolean behavior:
>>> i1.contains(6)
True
>>> i1.contains(15)
False
>>> i1.intersects(Interval(7, 18))
True
>>> i1.intersects(Interval(25, 29))
False
>>> print(Session().query(Interval).filter(Interval.contains(15)))
SELECT interval.id AS interval_id, interval.start AS interval_start,
interval."end" AS interval_end
FROM interval
WHERE interval.start <= :start_1 AND interval."end" > :end_1
>>> ia = aliased(Interval)
>>> print(Session().query(Interval, ia).filter(Interval.intersects(ia)))
SELECT interval.id AS interval_id, interval.start AS interval_start,
interval."end" AS interval_end, interval_1.id AS interval_1_id,
interval_1.start AS interval_1_start, interval_1."end" AS interval_1_end
FROM interval, interval AS interval_1
WHERE interval.start <= interval_1.start
AND interval."end" > interval_1.start
OR interval.start <= interval_1."end"
AND interval."end" > interval_1."end"
Our usage of the &
and |
bitwise operators above was fortunate, considering our functions operated on two boolean values to return a new one. In many cases, the construction of an in-Python function and a SQLAlchemy SQL expression have enough differences that two separate Python expressions should be defined. The decorators define the hybrid_property.expression() modifier for this purpose. As an example we’ll define the radius of the interval, which requires the usage of the absolute value function:
from sqlalchemy import func
class Interval:
# ...
@hybrid_property
def radius(self):
return abs(self.length) / 2
@radius.expression
def radius(cls):
return func.abs(cls.length) / 2
Above the Python function abs()
is used for instance-level operations, the SQL function ABS()
is used via the object for class-level expressions:
>>> i1.radius
2
>>> print(Session().query(Interval).filter(Interval.radius > 5))
SELECT interval.id AS interval_id, interval.start AS interval_start,
interval."end" AS interval_end
FROM interval
WHERE abs(interval."end" - interval.start) / :abs_1 > :param_1
Note
When defining an expression for a hybrid property or method, the expression method must retain the name of the original hybrid, else the new hybrid with the additional state will be attached to the class with the non-matching name. To use the example above:
class Interval:
# ...
@hybrid_property
def radius(self):
return abs(self.length) / 2
# WRONG - the non-matching name will cause this function to be
# ignored
@radius.expression
def radius_expression(cls):
return func.abs(cls.length) / 2
This is also true for other mutator methods, such as hybrid_property.update_expression(). This is the same behavior as that of the @property
construct that is part of standard Python.
Defining Setters
Hybrid properties can also define setter methods. If we wanted length
above, when set, to modify the endpoint value:
class Interval:
# ...
@hybrid_property
def length(self):
return self.end - self.start
@length.setter
def length(self, value):
self.end = self.start + value
The length(self, value)
method is now called upon set:
>>> i1 = Interval(5, 10)
>>> i1.length
5
>>> i1.length = 12
>>> i1.end
17
A hybrid can define a custom “UPDATE” handler for when using the method, allowing the hybrid to be used in the SET clause of the update.
Normally, when using a hybrid with Query.update(), the SQL expression is used as the column that’s the target of the SET. If our Interval
class had a hybrid start_point
that linked to Interval.start
, this could be substituted directly:
session.query(Interval).update({Interval.start_point: 10})
However, when using a composite hybrid like Interval.length
, this hybrid represents more than one column. We can set up a handler that will accommodate a value passed to which can affect this, using the hybrid_property.update_expression() decorator. A handler that works similarly to our setter would be:
Above, if we use Interval.length
in an UPDATE expression as:
session.query(Interval).update(
{Interval.length: 25}, synchronize_session='fetch')
We’ll get an UPDATE statement along the lines of:
UPDATE interval SET end=start + :value
In some cases, the default “evaluate” strategy can’t perform the SET expression in Python; while the addition operator we’re using above is supported, for more complex SET expressions it will usually be necessary to use either the “fetch” or False synchronization strategy as illustrated above.
Note
For ORM bulk updates to work with hybrids, the function name of the hybrid must match that of how it is accessed. Something like this wouldn’t work:
class Interval:
# ...
def _get(self):
return self.end - self.start
def _set(self, value):
self.end = self.start + value
def _update_expr(cls, value):
return [
(cls.end, cls.start + value)
]
length = hybrid_property(
fget=_get, fset=_set, update_expr=_update_expr
)
The Python descriptor protocol does not provide any reliable way for
a descriptor to know what attribute name it was accessed as, and
the UPDATE scheme currently relies upon being able to access the
attribute from an instance by name in order to perform the instance
New in version 1.2: added support for bulk updates to hybrid properties.
Working with Relationships
There’s no essential difference when creating hybrids that work with related objects as opposed to column-based data. The need for distinct expressions tends to be greater. The two variants we’ll illustrate are the “join-dependent” hybrid, and the “correlated subquery” hybrid.
Consider the following declarative mapping which relates a User
to a SavingsAccount
:
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
class SavingsAccount(Base):
__tablename__ = 'account'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
balance = Column(Numeric(15, 5))
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
accounts = relationship("SavingsAccount", backref="owner")
@hybrid_property
def balance(self):
if self.accounts:
return self.accounts[0].balance
else:
return None
@balance.setter
def balance(self, value):
if not self.accounts:
account = Account(owner=self)
else:
account = self.accounts[0]
account.balance = value
@balance.expression
def balance(cls):
return SavingsAccount.balance
The above hybrid property balance
works with the first SavingsAccount
entry in the list of accounts for this user. The in-Python getter/setter methods can treat accounts
as a Python list available on self
.
However, at the expression level, it’s expected that the User
class will be used in an appropriate context such that an appropriate join to SavingsAccount
will be present:
>>> print(Session().query(User, User.balance).
... join(User.accounts).filter(User.balance > 5000))
SELECT "user".id AS user_id, "user".name AS user_name,
account.balance AS account_balance
FROM "user" JOIN account ON "user".id = account.user_id
WHERE account.balance > :balance_1
Note however, that while the instance level accessors need to worry about whether self.accounts
is even present, this issue expresses itself differently at the SQL expression level, where we basically would use an outer join:
>>> from sqlalchemy import or_
>>> print (Session().query(User, User.balance).outerjoin(User.accounts).
... filter(or_(User.balance < 5000, User.balance == None)))
SELECT "user".id AS user_id, "user".name AS user_name,
account.balance AS account_balance
FROM "user" LEFT OUTER JOIN account ON "user".id = account.user_id
WHERE account.balance < :balance_1 OR account.balance IS NULL
Correlated Subquery Relationship Hybrid
We can, of course, forego being dependent on the enclosing query’s usage of joins in favor of the correlated subquery, which can portably be packed into a single column expression. A correlated subquery is more portable, but often performs more poorly at the SQL level. Using the same technique illustrated at Using column_property, we can adjust our SavingsAccount
example to aggregate the balances for all accounts, and use a correlated subquery for the column expression:
from sqlalchemy import Column, Integer, ForeignKey, Numeric, String
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import select, func
Base = declarative_base()
class SavingsAccount(Base):
__tablename__ = 'account'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
balance = Column(Numeric(15, 5))
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
accounts = relationship("SavingsAccount", backref="owner")
@hybrid_property
def balance(self):
return sum(acc.balance for acc in self.accounts)
@balance.expression
def balance(cls):
return select(func.sum(SavingsAccount.balance)).\
where(SavingsAccount.user_id==cls.id).\
label('total_balance')
The above recipe will give us the balance
column which renders a correlated SELECT:
>>> print(s.query(User).filter(User.balance > 400))
SELECT "user".id AS user_id, "user".name AS user_name
FROM "user"
WHERE (SELECT sum(account.balance) AS sum_1
FROM account
WHERE account.user_id = "user".id) > :param_1
The hybrid property also includes a helper that allows construction of custom comparators. A comparator object allows one to customize the behavior of each SQLAlchemy expression operator individually. They are useful when creating custom types that have some highly idiosyncratic behavior on the SQL side.
Note
The hybrid_property.comparator() decorator introduced in this section replaces the use of the decorator. They cannot be used together.
The example class below allows case-insensitive comparisons on the attribute named word_insensitive
:
from sqlalchemy.ext.hybrid import Comparator, hybrid_property
from sqlalchemy import func, Column, Integer, String
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class CaseInsensitiveComparator(Comparator):
def __eq__(self, other):
return func.lower(self.__clause_element__()) == func.lower(other)
class SearchWord(Base):
__tablename__ = 'searchword'
id = Column(Integer, primary_key=True)
word = Column(String(255), nullable=False)
@hybrid_property
def word_insensitive(self):
return self.word.lower()
@word_insensitive.comparator
def word_insensitive(cls):
return CaseInsensitiveComparator(cls.word)
Above, SQL expressions against word_insensitive
will apply the LOWER()
SQL function to both sides:
>>> print(Session().query(SearchWord).filter_by(word_insensitive="Trucks"))
SELECT searchword.id AS searchword_id, searchword.word AS searchword_word
FROM searchword
WHERE lower(searchword.word) = lower(:lower_1)
The CaseInsensitiveComparator
above implements part of the ColumnOperators interface. A “coercion” operation like lowercasing can be applied to all comparison operations (i.e. eq
, lt
, gt
, etc.) using :
Reusing Hybrid Properties across Subclasses
A hybrid can be referred to from a superclass, to allow modifying methods like , hybrid_property.setter() to be used to redefine those methods on a subclass. This is similar to how the standard Python @property
object works:
class FirstNameOnly(Base):
# ...
first_name = Column(String)
@hybrid_property
def name(self):
return self.first_name
@name.setter
def name(self, value):
self.first_name = value
class FirstNameLastName(FirstNameOnly):
# ...
last_name = Column(String)
@FirstNameOnly.name.getter
def name(self):
return self.first_name + ' ' + self.last_name
@name.setter
def name(self, value):
self.first_name, self.last_name = value.split(' ', 1)
Above, the FirstNameLastName
class refers to the hybrid from FirstNameOnly.name
to repurpose its getter and setter for the subclass.
When overriding and hybrid_property.comparator() alone as the first reference to the superclass, these names conflict with the same-named accessors on the class- level object returned at the class level. To override these methods when referring directly to the parent class descriptor, add the special qualifier hybrid_property.overrides, which will de- reference the instrumented attribute back to the hybrid object:
class FirstNameLastName(FirstNameOnly):
# ...
last_name = Column(String)
@FirstNameOnly.name.overrides.expression
def name(cls):
return func.concat(cls.first_name, ' ', cls.last_name)
New in version 1.2: Added as well as the ability to redefine accessors per-subclass.
Note in our previous example, if we were to compare the word_insensitive
attribute of a SearchWord
instance to a plain Python string, the plain Python string would not be coerced to lower case - the CaseInsensitiveComparator
we built, being returned by @word_insensitive.comparator
, only applies to the SQL side.
A more comprehensive form of the custom comparator is to construct a Hybrid Value Object. This technique applies the target value or expression to a value object which is then returned by the accessor in all cases. The value object allows control of all operations upon the value as well as how compared values are treated, both on the SQL expression side as well as the Python value side. Replacing the previous CaseInsensitiveComparator
class with a new CaseInsensitiveWord
class:
class CaseInsensitiveWord(Comparator):
"Hybrid value representing a lower case representation of a word."
def __init__(self, word):
if isinstance(word, basestring):
self.word = word.lower()
elif isinstance(word, CaseInsensitiveWord):
self.word = word.word
else:
self.word = func.lower(word)
def operate(self, op, other, **kwargs):
if not isinstance(other, CaseInsensitiveWord):
other = CaseInsensitiveWord(other)
return op(self.word, other.word, **kwargs)
def __clause_element__(self):
return self.word
def __str__(self):
return self.word
key = 'word'
"Label to apply to Query tuple results"
Above, the CaseInsensitiveWord
object represents self.word
, which may be a SQL function, or may be a Python native. By overriding operate()
and __clause_element__()
to work in terms of self.word
, all comparison operations will work against the “converted” form of word
, whether it be SQL side or Python side. Our SearchWord
class can now deliver the CaseInsensitiveWord
object unconditionally from a single hybrid call:
class SearchWord(Base):
__tablename__ = 'searchword'
id = Column(Integer, primary_key=True)
word = Column(String(255), nullable=False)
@hybrid_property
def word_insensitive(self):
return CaseInsensitiveWord(self.word)
The word_insensitive
attribute now has case-insensitive comparison behavior universally, including SQL expression vs. Python expression (note the Python value is converted to lower case on the Python side here):
>>> print(Session().query(SearchWord).filter_by(word_insensitive="Trucks"))
SELECT searchword.id AS searchword_id, searchword.word AS searchword_word
FROM searchword
WHERE lower(searchword.word) = :lower_1
SQL expression versus SQL expression:
>>> sw1 = aliased(SearchWord)
>>> sw2 = aliased(SearchWord)
>>> print(Session().query(
... sw1.word_insensitive,
... sw2.word_insensitive).\
... filter(
... sw1.word_insensitive > sw2.word_insensitive
... ))
SELECT lower(searchword_1.word) AS lower_1,
lower(searchword_2.word) AS lower_2
FROM searchword AS searchword_1, searchword AS searchword_2
WHERE lower(searchword_1.word) > lower(searchword_2.word)
Python only expression:
>>> ws1 = SearchWord(word="SomeWord")
>>> ws1.word_insensitive == "sOmEwOrD"
True
>>> ws1.word_insensitive == "XOmEwOrX"
False
>>> print(ws1.word_insensitive)
someword
The Hybrid Value pattern is very useful for any kind of value that may have multiple representations, such as timestamps, time deltas, units of measurement, currencies and encrypted passwords.
See also
- on the techspot.zzzeek.org blog
Value Agnostic Types, Part II - on the techspot.zzzeek.org blog
API Reference
class sqlalchemy.ext.hybrid.hybrid_method
A decorator which allows definition of a Python object method with both instance-level and class-level behavior.
Members
Class signature
class sqlalchemy.ext.hybrid.hybrid_method (, typing.Generic
)
method sqlalchemy.ext.hybrid.hybrid_method.__init__(func: Callable[[Concatenate[Any, _P]], _R], expr: Optional[Callable[[Concatenate[Any, _P]], SQLCoreOperations[_R]]] = None)
Create a new .
Usage is typically via decorator:
from sqlalchemy.ext.hybrid import hybrid_method
class SomeClass:
@hybrid_method
def value(self, x, y):
return self._value + x + y
@value.expression
def value(self, x, y):
return func.some_function(self._value, x, y)
method sqlalchemy.ext.hybrid.hybrid_method.expression(expr: Callable[[Concatenate[Any, _P]], SQLCoreOperations[_R]]) → [_P, _R]
Provide a modifying decorator that defines a SQL-expression producing method.
attribute sqlalchemy.ext.hybrid.hybrid_method.extension_type: = ‘HYBRID_METHOD’
The extension type, if any. Defaults to
NotExtension.NOT_EXTENSION
See also
attribute sqlalchemy.ext.hybrid.hybrid_method.is_attribute = True
True if this object is a Python .
This can refer to one of many types. Usually a QueryableAttribute which handles attributes events on behalf of a . But can also be an extension type such as AssociationProxy or . The InspectionAttr.extension_type will refer to a constant identifying the specific subtype.
See also
class sqlalchemy.ext.hybrid.hybrid_property
A decorator which allows definition of a Python descriptor with both instance-level and class-level behavior.
Members
__init__(), , deleter(), , extension_type, , is_attribute, , setter(),
Class signature
class sqlalchemy.ext.hybrid.hybrid_property (, sqlalchemy.orm.base.ORMDescriptor
)
method sqlalchemy.ext.hybrid.hybrid_property.__init__(fget: _HybridGetterType[_T], fset: Optional[_HybridSetterType[_T]] = None, fdel: Optional[_HybridDeleterType[_T]] = None, expr: Optional[_HybridExprCallableType[_T]] = None, custom_comparator: Optional[[_T]] = None, update_expr: Optional[_HybridUpdaterType[_T]] = None)
Create a new hybrid_property.
Usage is typically via decorator:
from sqlalchemy.ext.hybrid import hybrid_property
class SomeClass:
@hybrid_property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value
method comparator(comparator: Comparator[_T]) → [_T]
Provide a modifying decorator that defines a custom comparator producing method.
The return value of the decorated method should be an instance of Comparator.
Note
The decorator replaces the use of the hybrid_property.expression() decorator. They cannot be used together.
When a hybrid is invoked at the class level, the object given here is wrapped inside of a specialized QueryableAttribute, which is the same kind of object used by the ORM to represent other mapped attributes. The reason for this is so that other class-level attributes such as docstrings and a reference to the hybrid itself may be maintained within the structure that’s returned, without any modifications to the original comparator object passed in.
Note
When referring to a hybrid property from an owning class (e.g.
SomeClass.some_hybrid
), an instance of is returned, representing the expression or comparator object as this hybrid object. However, that object itself has accessors calledexpression
andcomparator
; so when attempting to override these decorators on a subclass, it may be necessary to qualify it using the hybrid_property.overrides modifier first. See that modifier for details.method deleter(fdel: _HybridDeleterType[_T]) → hybrid_property[_T]
Provide a modifying decorator that defines a deletion method.
method expression(expr: _HybridExprCallableType[_T]) → hybrid_property[_T]
Provide a modifying decorator that defines a SQL-expression producing method.
When a hybrid is invoked at the class level, the SQL expression given here is wrapped inside of a specialized , which is the same kind of object used by the ORM to represent other mapped attributes. The reason for this is so that other class-level attributes such as docstrings and a reference to the hybrid itself may be maintained within the structure that’s returned, without any modifications to the original SQL expression passed in.
When referring to a hybrid property from an owning class (e.g.
SomeClass.some_hybrid
), an instance of QueryableAttribute is returned, representing the expression or comparator object as well as this hybrid object. However, that object itself has accessors calledexpression
andcomparator
; so when attempting to override these decorators on a subclass, it may be necessary to qualify it using the modifier first. See that modifier for details.See also
Defining Expression Behavior Distinct from Attribute Behavior
attribute extension_type: InspectionAttrExtensionType = ‘HYBRID_PROPERTY’
The extension type, if any. Defaults to
NotExtension.NOT_EXTENSION
See also
method getter(fget: _HybridGetterType[_T]) → hybrid_property[_T]
Provide a modifying decorator that defines a getter method.
New in version 1.2.
attribute is_attribute = True
True if this object is a Python descriptor.
This can refer to one of many types. Usually a which handles attributes events on behalf of a MapperProperty. But can also be an extension type such as or hybrid_property. The will refer to a constant identifying the specific subtype.
See also
attribute overrides
Prefix for a method that is overriding an existing attribute.
The hybrid_property.overrides accessor just returns this hybrid object, which when called at the class level from a parent class, will de-reference the “instrumented attribute” normally returned at this level, and allow modifying decorators like and hybrid_property.comparator() to be used without conflicting with the same-named attributes normally present on the :
class SuperClass:
# ...
@hybrid_property
def foobar(self):
return self._foobar
class SubClass(SuperClass):
# ...
@SuperClass.foobar.overrides.expression
def foobar(cls):
return func.subfoobar(self._foobar)
New in version 1.2.
See also
method setter(fset: _HybridSetterType[_T]) → hybrid_property[_T]
Provide a modifying decorator that defines a setter method.
method update_expression(meth: _HybridUpdaterType[_T]) → hybrid_property[_T]
Provide a modifying decorator that defines an UPDATE tuple producing method.
The method accepts a single value, which is the value to be rendered into the SET clause of an UPDATE statement. The method should then process this value into individual column expressions that fit into the ultimate SET clause, and return them as a sequence of 2-tuples. Each tuple contains a column expression as the key and a value to be rendered.
E.g.:
New in version 1.2.
class sqlalchemy.ext.hybrid.Comparator
A helper class that allows easy construction of custom classes for usage with hybrids.
Class signature
class sqlalchemy.ext.hybrid.Comparator ()
class sqlalchemy.ext.hybrid.HybridExtensionType
An enumeration.
Members
Class signature
class sqlalchemy.ext.hybrid.HybridExtensionType ()
attribute sqlalchemy.ext.hybrid.HybridExtensionType.HYBRID_METHOD = ‘HYBRID_METHOD’
Symbol indicating an
InspectionAttr
that’s of type .Is assigned to the InspectionAttr.extension_type attribute.
See also
Mapper.all_orm_attributes
attribute HYBRID_PROPERTY = ‘HYBRID_PROPERTY’
Symbol indicating an that’s
of type hybrid_method.
Is assigned to the attribute.
See also
Mapper.all_orm_attributes