]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Column._set_parent will complete the key==name contract for instances constructed...
authorJason Kirtland <jek@discorporate.us>
Tue, 18 Mar 2008 03:07:54 +0000 (03:07 +0000)
committerJason Kirtland <jek@discorporate.us>
Tue, 18 Mar 2008 03:07:54 +0000 (03:07 +0000)
lib/sqlalchemy/schema.py
test/sql/columns.py

index c3db8bfad3da00d37edbd6d05ba75474b5f7fcc7..a393c160fd1c238331d97883df6aecc7109c525a 100644 (file)
@@ -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!")
index 0a904bee1423673dbedae135b4b999aeee4d16f5..76bf9b389c6f41ef5b485f77ff036f64d823866e 100644 (file)
@@ -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,