From 80b6591dbea86f87cc845c67ea11c1449ee75eee Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 17 Dec 2013 15:38:35 -0500 Subject: - The :func:`.cast` function, when given a plain literal value, will now apply the given type to the given literal value on the bind parameter side according to the type given to the cast. This essentially replaces what would normally be the detected type of the literal value. This only takes effect if the auto-detected type of the literal value is either "nulltype" (e.g. couldn't detect) or a type that is of the same "affinity" as the cast type. The net change here is that the :func:`.cast` function includes more of the functionality already present in the :func:`.type_coerce` function. --- lib/sqlalchemy/sql/elements.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql/elements.py') diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 69e365bd3..91ce0a090 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -1753,7 +1753,10 @@ class Cast(ColumnElement): """ self.type = type_api.to_instance(totype) self.clause = _literal_as_binds(clause, None) - if isinstance(self.clause, BindParameter) and self.clause.type._isnull: + if isinstance(self.clause, BindParameter) and ( + self.clause.type._isnull + or self.clause.type._type_affinity is self.type._type_affinity + ): self.clause = self.clause._clone() self.clause.type = self.type -- cgit v1.2.1