summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/elements.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-12-17 15:38:35 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2013-12-17 15:38:35 -0500
commit80b6591dbea86f87cc845c67ea11c1449ee75eee (patch)
tree4f49591d79138718c02b733f24d565a75de339b7 /lib/sqlalchemy/sql/elements.py
parentd98fcca0b3441e09c3d56ad69c93b41f9b240f0f (diff)
downloadsqlalchemy-80b6591dbea86f87cc845c67ea11c1449ee75eee.tar.gz
- 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.
Diffstat (limited to 'lib/sqlalchemy/sql/elements.py')
-rw-r--r--lib/sqlalchemy/sql/elements.py5
1 files changed, 4 insertions, 1 deletions
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