Represent a ``CAST`` expression. :class:`.Cast` is produced using the :func:`.cast` factory function, as in:: from sqlalchemy import cast, Numeric stmt = select(cast(product_table.c.unit_price, Numeric(10, 4))) Details on :class:`.Cast` usage is at :func:`.cast`.
| 3678 | |
| 3679 | |
| 3680 | class Cast(WrapsColumnExpression[_T]): |
| 3681 | """Represent a ``CAST`` expression. |
| 3682 | |
| 3683 | :class:`.Cast` is produced using the :func:`.cast` factory function, |
| 3684 | as in:: |
| 3685 | |
| 3686 | from sqlalchemy import cast, Numeric |
| 3687 | |
| 3688 | stmt = select(cast(product_table.c.unit_price, Numeric(10, 4))) |
| 3689 | |
| 3690 | Details on :class:`.Cast` usage is at :func:`.cast`. |
| 3691 | |
| 3692 | .. seealso:: |
| 3693 | |
| 3694 | :ref:`tutorial_casts` |
| 3695 | |
| 3696 | :func:`.cast` |
| 3697 | |
| 3698 | :func:`.try_cast` |
| 3699 | |
| 3700 | :func:`.type_coerce` - an alternative to CAST that coerces the type |
| 3701 | on the Python side only, which is often sufficient to generate the |
| 3702 | correct SQL and data coercion. |
| 3703 | |
| 3704 | """ |
| 3705 | |
| 3706 | __visit_name__ = "cast" |
| 3707 | |
| 3708 | _traverse_internals: _TraverseInternalsType = [ |
| 3709 | ("clause", InternalTraversal.dp_clauseelement), |
| 3710 | ("type", InternalTraversal.dp_type), |
| 3711 | ] |
| 3712 | |
| 3713 | clause: ColumnElement[Any] |
| 3714 | type: TypeEngine[_T] |
| 3715 | typeclause: TypeClause |
| 3716 | |
| 3717 | def __init__( |
| 3718 | self, |
| 3719 | expression: _ColumnExpressionArgument[Any], |
| 3720 | type_: _TypeEngineArgument[_T], |
| 3721 | ): |
| 3722 | self.type = type_api.to_instance(type_) |
| 3723 | self.clause = coercions.expect( |
| 3724 | roles.ExpressionElementRole, |
| 3725 | expression, |
| 3726 | type_=self.type, |
| 3727 | apply_propagate_attrs=self, |
| 3728 | ) |
| 3729 | self.typeclause = TypeClause(self.type) |
| 3730 | |
| 3731 | @util.ro_non_memoized_property |
| 3732 | def _from_objects(self) -> List[FromClause]: |
| 3733 | return self.clause._from_objects |
| 3734 | |
| 3735 | @property |
| 3736 | def wrapped_column_expression(self): |
| 3737 | return self.clause |