SQL Expressions as Mapped Attributes

    The easiest and most flexible way to link relatively simple SQL expressions to a class is to use a so-called “hybrid attribute”, described in the section . The hybrid provides for an expression that works at both the Python level as well as at the SQL expression level. For example, below we map a class User, containing attributes firstname and lastname, and include a hybrid that will provide for us the fullname, which is the string concatenation of the two:

    Above, the fullname attribute is interpreted at both the instance and class level, so that it is available from an instance:

    1. some_user = session.scalars(select(User).limit(1)).first()
    2. print(some_user.fullname)

    as well as usable within queries:

    1. some_user = session.scalars(
    2. select(User).where(User.fullname == "John Smith").limit(1)
    3. ).first()

    The string concatenation example is a simple one, where the Python expression can be dual purposed at the instance and class level. Often, the SQL expression must be distinguished from the Python expression, which can be achieved using hybrid_property.expression(). Below we illustrate the case where a conditional needs to be present inside the hybrid, using the if statement in Python and the construct for SQL expressions:

    1. from sqlalchemy.ext.hybrid import hybrid_property
    2. from sqlalchemy.sql import case
    3. class User(Base):
    4. __tablename__ = "user"
    5. id = mapped_column(Integer, primary_key=True)
    6. firstname = mapped_column(String(50))
    7. lastname = mapped_column(String(50))
    8. @hybrid_property
    9. def fullname(self):
    10. if self.firstname is not None:
    11. return self.firstname + " " + self.lastname
    12. else:
    13. return self.lastname
    14. @fullname.expression
    15. def fullname(cls):
    16. return case(
    17. [
    18. (cls.firstname != None, cls.firstname + " " + cls.lastname),
    19. ],
    20. else_=cls.lastname,
    21. )

    The function can be used to map a SQL expression in a manner similar to a regularly mapped Column. With this technique, the attribute is loaded along with all other column-mapped attributes at load time. This is in some cases an advantage over the usage of hybrids, as the value can be loaded up front at the same time as the parent row of the object, particularly if the expression is one which links to other tables (typically as a correlated subquery) to access data that wouldn’t normally be available on an already loaded object.

    Disadvantages to using for SQL expressions include that the expression must be compatible with the SELECT statement emitted for the class as a whole, and there are also some configurational quirks which can occur when using column_property() from declarative mixins.

    Our “fullname” example can be expressed using as follows:

    1. from sqlalchemy.orm import column_property
    2. from sqlalchemy import select, func
    3. from sqlalchemy import Column, Integer, String, ForeignKey
    4. from sqlalchemy.orm import DeclarativeBase
    5. class Base(DeclarativeBase):
    6. pass
    7. class Address(Base):
    8. __tablename__ = "address"
    9. user_id = mapped_column(Integer, ForeignKey("user.id"))
    10. class User(Base):
    11. __tablename__ = "user"
    12. id = mapped_column(Integer, primary_key=True)
    13. address_count = column_property(
    14. select(func.count(Address.id))
    15. .where(Address.user_id == id)
    16. .correlate_except(Address)
    17. .scalar_subquery()
    18. )

    In the above example, we define a ScalarSelect() construct like the following:

    1. stmt = (
    2. select(func.count(Address.id))
    3. .where(Address.user_id == id)
    4. .correlate_except(Address)
    5. .scalar_subquery()
    6. )

    Above, we first use to create a Select construct, which we then convert into a using the Select.scalar_subquery() method, indicating our intent to use this statement in a column expression context.

    Within the Select itself, we select the count of Address.id rows where the Address.user_id column is equated to id, which in the context of the User class is the named id (note that id is also the name of a Python built in function, which is not what we want to use here - if we were outside of the User class definition, we’d use User.id).

    The Select.correlate_except() method indicates that each element in the FROM clause of this may be omitted from the FROM list (that is, correlated to the enclosing SELECT statement against User) except for the one corresponding to Address. This isn’t strictly necessary, but prevents Address from being inadvertently omitted from the FROM list in the case of a long string of joins between User and Address tables where SELECT statements against Address are nested.

    For a column_property() that refers to columns linked from a many-to-many relationship, use to join the fields of the association table to both tables in a relationship:

    1. from sqlalchemy import and_
    2. class Author(Base):
    3. # ...
    4. book_count = column_property(
    5. select(func.count(books.c.id))
    6. .where(
    7. and_(
    8. book_authors.c.author_id == authors.c.id,
    9. book_authors.c.book_id == books.c.id,
    10. )
    11. )
    12. .scalar_subquery()
    13. )

    If import issues prevent the from being defined inline with the class, it can be assigned to the class after both are configured. When using mappings that make use of a Declarative base class (i.e. produced by the DeclarativeBase superclass or legacy functions such as ), this attribute assignment has the effect of calling Mapper.add_property() to add an additional property after the fact:

    When using mapping styles that don’t use Declarative base classes such as the decorator, the Mapper.add_property() method may be invoked explicitly on the underlying object, which can be obtained using inspect():

    1. from sqlalchemy.orm import registry
    2. @reg.mapped
    3. class User:
    4. __tablename__ = "user"
    5. # ... additional mapping directives
    6. # later ...
    7. # works for any kind of mapping
    8. from sqlalchemy import inspect
    9. inspect(User).add_property(
    10. column_property(
    11. select(func.count(Address.id))
    12. .where(Address.user_id == User.id)
    13. .scalar_subquery()
    14. )
    15. )

    It is possible to create mappings that combine multiple objects together. The ColumnProperty will be interpreted as a SQL expression when used in a Core expression context, provided that it is targeted by an existing expression object; this works by the Core detecting that the object has a __clause_element__() method which returns a SQL expression. However, if the is used as a lead object in an expression where there is no other Core SQL expression object to target it, the ColumnProperty.expression attribute will return the underlying SQL expression so that it can be used to build SQL expressions consistently. Below, the File class contains an attribute File.path that concatenates a string token to the File.filename attribute, which is itself a :

    1. class File(Base):
    2. __tablename__ = "file"
    3. id = mapped_column(Integer, primary_key=True)
    4. name = mapped_column(String(64))
    5. extension = mapped_column(String(8))
    6. filename = column_property(name + "." + extension)
    7. path = column_property("C:/" + filename.expression)

    When the File class is used in expressions normally, the attributes assigned to filename and path are usable directly. The use of the ColumnProperty.expression attribute is only necessary when using the directly within the mapping definition:

    1. stmt = select(File.path).where(File.filename == "foo.txt")

    The column deferral feature introduced in the at Limiting which Columns Load with Column Deferral may be applied at mapping time to a SQL expression mapped by by using the deferred() function in place of :

    See also

    Using deferred() for imperative mappers, mapped SQL expressions

    In cases where a SQL query more elaborate than what column_property() or can provide must be emitted, a regular Python function accessed as an attribute can be used, assuming the expression only needs to be available on an already-loaded instance. The function is decorated with Python’s own @property decorator to mark it as a read-only attribute. Within the function, object_session() is used to locate the corresponding to the current object, which is then used to emit a query:

    1. from sqlalchemy.orm import object_session
    2. from sqlalchemy import select, func
    3. class User(Base):
    4. __tablename__ = "user"
    5. id = mapped_column(Integer, primary_key=True)
    6. firstname = mapped_column(String(50))
    7. lastname = mapped_column(String(50))
    8. @property
    9. def address_count(self):
    10. return object_session(self).scalar(
    11. )

    In addition to being able to configure fixed SQL expressions on mapped classes, the SQLAlchemy ORM also includes a feature wherein objects may be loaded with the results of arbitrary SQL expressions which are set up at query time as part of their state. This behavior is available by configuring an ORM mapped attribute using and then using the with_expression() loader option at query time. See the section for an example mapping and usage.