From 031c500ff4c69bb43914cda707796f15e6b132c2 Mon Sep 17 00:00:00 2001 From: Jason Kirtland Date: Tue, 18 Mar 2008 03:07:54 +0000 Subject: [PATCH] - Column._set_parent will complete the key==name contract for instances constructed anonymously --- lib/sqlalchemy/schema.py | 12 +++++++++--- test/sql/columns.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index c3db8bfad3..a393c160fd 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -412,7 +412,9 @@ class Column(SchemaItem, expression._ColumnClause): name The name of this column. This should be the identical name as it - appears, or will appear, in the database. + appears, or will appear, in the database. Name may be omitted at + construction time but must be assigned before adding a Column + instance to a Table. type\_ The ``TypeEngine`` for this column. This can be any subclass of @@ -430,8 +432,10 @@ class Column(SchemaItem, expression._ColumnClause): Keyword arguments include: key - Defaults to None: an optional *alias name* for this column. The - column will then be identified everywhere in an application, + Defaults to the column name: a Python-only *alias name* for this + column. + + The column will then be identified everywhere in an application, including the column list on its Table, by this key, and not the given name. Generated SQL, however, will still reference the column by its actual name. @@ -583,6 +587,8 @@ class Column(SchemaItem, expression._ColumnClause): raise exceptions.ArgumentError( "Column must be constructed with a name or assign .name " "before adding to a Table.") + if self.key is None: + self.key = self.name self.metadata = table.metadata if getattr(self, 'table', None) is not None: raise exceptions.ArgumentError("this Column already has a table!") diff --git a/test/sql/columns.py b/test/sql/columns.py index 0a904bee14..76bf9b389c 100644 --- a/test/sql/columns.py +++ b/test/sql/columns.py @@ -39,6 +39,18 @@ class ColumnDefinitionTest(TestBase): self.assertRaises(exceptions.ArgumentError, Table, 't', MetaData(), *c) + def test_incomplete_key(self): + c = Column(Integer) + assert c.name is None + assert c.key is None + + c.name = 'named' + t = Table('t', MetaData(), c) + + assert c.name == 'named' + assert c.name == c.key + + def test_bogus(self): self.assertRaises(exceptions.ArgumentError, Column, 'foo', name='bar') self.assertRaises(exceptions.ArgumentError, Column, 'foo', Integer, -- 2.47.3