From 003149c504149849c679a60a1f346a0f0393dce0 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 18 Sep 2010 13:18:44 -0400 Subject: - An informative error message is raised if a Column which has not yet been assigned a name, i.e. as in declarative, is used in a context where it is exported to the columns collection of an enclosing select() construct, or if any construct involving that column is compiled before its name is assigned. [ticket:1862] --- lib/sqlalchemy/sql/compiler.py | 4 ++++ lib/sqlalchemy/sql/expression.py | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index fcff5e355..e47db7e28 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -332,6 +332,10 @@ class SQLCompiler(engine.Compiled): def visit_column(self, column, result_map=None, **kwargs): name = column.name + if name is None: + raise exc.CompileError("Cannot compile Column object until " + "it's 'name' is assigned.") + if not column.is_literal and isinstance(name, sql._generated_label): name = self._truncated_identifier("colident", name) diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 1b1cfee8a..70d5f13fc 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -1852,17 +1852,19 @@ class ColumnElement(ClauseElement, _CompareMixin): descending selectable. """ - - if name: - co = ColumnClause(name, selectable, type_=getattr(self, - 'type', None)) + if name is None: + name = self.anon_label + # TODO: may want to change this to anon_label, + # or some value that is more useful than the + # compiled form of the expression + key = str(self) else: - name = str(self) - co = ColumnClause(self.anon_label, selectable, - type_=getattr(self, 'type', None)) - + key = name + + co = ColumnClause(name, selectable, type_=getattr(self, + 'type', None)) co.proxies = [self] - selectable.columns[name] = co + selectable.columns[key] = co return co def compare(self, other, use_proxies=False, equivalents=None, **kw): -- cgit v1.2.1