r"""Apply an ad-hoc SQL expression to a "deferred expression" attribute. This option is used in conjunction with the :func:`_orm.query_expression` mapper-level construct that indicates an attribute which should be the target of an ad-hoc SQL expression. E.g.
(
self,
key: _AttrType,
expression: _ColumnExpressionArgument[Any],
)
| 720 | ) |
| 721 | |
| 722 | def with_expression( |
| 723 | self, |
| 724 | key: _AttrType, |
| 725 | expression: _ColumnExpressionArgument[Any], |
| 726 | ) -> Self: |
| 727 | r"""Apply an ad-hoc SQL expression to a "deferred expression" |
| 728 | attribute. |
| 729 | |
| 730 | This option is used in conjunction with the |
| 731 | :func:`_orm.query_expression` mapper-level construct that indicates an |
| 732 | attribute which should be the target of an ad-hoc SQL expression. |
| 733 | |
| 734 | E.g.:: |
| 735 | |
| 736 | stmt = select(SomeClass).options( |
| 737 | with_expression(SomeClass.x_y_expr, SomeClass.x + SomeClass.y) |
| 738 | ) |
| 739 | |
| 740 | :param key: Attribute to be populated |
| 741 | |
| 742 | :param expr: SQL expression to be applied to the attribute. |
| 743 | |
| 744 | .. seealso:: |
| 745 | |
| 746 | :ref:`orm_queryguide_with_expression` - background and usage |
| 747 | examples |
| 748 | |
| 749 | """ |
| 750 | |
| 751 | expression = _orm_full_deannotate( |
| 752 | coercions.expect(roles.LabeledColumnExprRole, expression) |
| 753 | ) |
| 754 | |
| 755 | return self._set_column_strategy( |
| 756 | (key,), {"query_expression": True}, extra_criteria=(expression,) |
| 757 | ) |
| 758 | |
| 759 | def selectin_polymorphic(self, classes: Iterable[Type[Any]]) -> Self: |
| 760 | """Indicate an eager load should take place for all attributes |