diff options
-rw-r--r-- | lib/sqlalchemy/sql/elements.py | 7 | ||||
-rw-r--r-- | test/sql/test_type_expressions.py | 7 |
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index eb0923e15..4af1e4463 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -844,6 +844,13 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): else: return False + def cast(self, type_): + """Produce a type cast, i.e. ``CAST(<expression> AS <type>)``. + + This is a shortcut to the :func:`.cast` function. + """ + return Cast(self, type_) + def label(self, name): """Produce a column label, i.e. ``<columnname> AS <name>``. diff --git a/test/sql/test_type_expressions.py b/test/sql/test_type_expressions.py index 574edfe9e..c35e9ff53 100644 --- a/test/sql/test_type_expressions.py +++ b/test/sql/test_type_expressions.py @@ -40,6 +40,13 @@ class SelectTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL): "SELECT CAST(test_table.y AS VARCHAR) AS anon_1 FROM test_table" ) + def test_cast_method(self): + table = self._fixture() + self.assert_compile( + select([table.c.y.cast(String)]), + "SELECT CAST(test_table.y AS VARCHAR) AS anon_1 FROM test_table" + ) + def test_select_cols_use_labels(self): table = self._fixture() |