summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-10-21 15:06:41 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-10-21 15:06:41 -0400
commit56976624169af6d0d329b4834ee9caa7243573dc (patch)
treef57c8fe739c3213e87a686f002b8e66712991a6f
parent4663ec98b226a7d495846f0d89c646110705bb30 (diff)
downloadsqlalchemy-56976624169af6d0d329b4834ee9caa7243573dc.tar.gz
- Fixed bug where :func:`.type_coerce` would not interpret ORM
elements with a ``__clause_element__()`` method properly. [ticket:2849]
-rw-r--r--doc/build/changelog/changelog_08.rst8
-rw-r--r--lib/sqlalchemy/sql/elements.py4
-rw-r--r--test/sql/test_types.py11
3 files changed, 21 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst
index 727931f9a..ab36c76f8 100644
--- a/doc/build/changelog/changelog_08.rst
+++ b/doc/build/changelog/changelog_08.rst
@@ -12,6 +12,14 @@
.. change::
:tags: bug, sql
+ :tickets: 2849
+ :versions: 0.9.0
+
+ Fixed bug where :func:`.type_coerce` would not interpret ORM
+ elements with a ``__clause_element__()`` method properly.
+
+ .. change::
+ :tags: bug, sql
:tickets: 2842
:versions: 0.9.0
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index f70496418..251102d59 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -117,8 +117,8 @@ def type_coerce(expr, type_):
"""
type_ = type_api.to_instance(type_)
- if hasattr(expr, '__clause_expr__'):
- return type_coerce(expr.__clause_expr__())
+ if hasattr(expr, '__clause_element__'):
+ return type_coerce(expr.__clause_element__(), type_)
elif isinstance(expr, BindParameter):
bp = expr._clone()
bp.type = type_
diff --git a/test/sql/test_types.py b/test/sql/test_types.py
index a2791ee29..f739707f3 100644
--- a/test/sql/test_types.py
+++ b/test/sql/test_types.py
@@ -466,6 +466,17 @@ class UserDefinedTest(fixtures.TablesTest, AssertsCompiledSQL):
'd1BIND_OUT'
)
+ class MyFoob(object):
+ def __clause_element__(self):
+ return t.c.data
+
+ eq_(
+ testing.db.execute(
+ select([t.c.data, type_coerce(MyFoob(), MyType)])
+ ).fetchall(),
+ [('d1', 'd1BIND_OUT')]
+ )
+
@classmethod
def define_tables(cls, metadata):
class MyType(types.UserDefinedType):