]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The copy() method on Column now supports uninitialized,
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 2 Jan 2010 03:23:33 +0000 (03:23 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 2 Jan 2010 03:23:33 +0000 (03:23 +0000)
unnamed Column objects. This allows easy creation of
declarative helpers which place common columns on multiple
subclasses.

CHANGES
lib/sqlalchemy/schema.py
test/engine/test_metadata.py

diff --git a/CHANGES b/CHANGES
index 449efdde5d7c4701f1a6afb07c8f668a4463f318..305ca2b9882b3268f2bd498a268c8d92c1aebba8 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,12 @@ CHANGES
 
 0.5.8
 =====
+- sql
+    - The copy() method on Column now supports uninitialized,
+      unnamed Column objects. This allows easy creation of 
+      declarative helpers which place common columns on multiple 
+      subclasses.
+      
 - postgresql
     - The extract() function, which was slightly improved in
       0.5.7, needed a lot more work to generate the correct
index 7ba835cc4464390f1ca89be6f4e727677c42c5b2..ce095799b0f2deb55798728cd9c4c2b05b49b044 100644 (file)
@@ -723,10 +723,21 @@ class Column(SchemaItem, expression.ColumnClause):
         This is used in ``Table.tometadata``.
 
         """
+        
+        if self.args:
+            # if not Table initialized
+            args = []
+            for a in self.args:
+                if isinstance(a, Constraint):
+                    a = a.copy()
+            args.append(a)
+        else:
+            # if Table initialized
+            args = [c.copy(**kw) for c in self.constraints]
+            
         return Column(
-                self.name, 
-                self.type, 
-                self.default, 
+                name=self.name, 
+                type_=self.type, 
                 key = self.key, 
                 primary_key = self.primary_key, 
                 nullable = self.nullable, 
@@ -737,7 +748,8 @@ class Column(SchemaItem, expression.ColumnClause):
                 server_default=self.server_default,
                 onupdate=self.onupdate,
                 server_onupdate=self.server_onupdate,
-                *[c.copy(**kw) for c in self.constraints])
+                *args
+                )
 
     def _make_proxy(self, selectable, name=None):
         """Create a *proxy* for this column.
index 8680f31ea938ef024e1a099f6ec8a626913d87b3..6f1a45e5da9608de4084cca792bbb690fa7528a7 100644 (file)
@@ -1,7 +1,7 @@
 from sqlalchemy.test.testing import assert_raises, assert_raises_message
 import pickle
 from sqlalchemy import MetaData
-from sqlalchemy import Integer, String, UniqueConstraint, CheckConstraint, ForeignKey
+from sqlalchemy import Integer, String, UniqueConstraint, CheckConstraint, ForeignKey, Sequence
 from sqlalchemy.test.schema import Table
 from sqlalchemy.test.schema import Column
 import sqlalchemy as tsa
@@ -20,7 +20,24 @@ class MetaDataTest(TestBase, ComparesTables):
         finally:
             metadata.drop_all()
 
-
+    def test_uninitialized_column_copy(self):
+        for col in [
+            Column('foo', String(), nullable=False),
+            Column(Integer(), primary_key=True),
+            Column('bar', Integer(), Sequence('foo_seq'), primary_key=True, key='bar'),
+            Column(Integer(), ForeignKey('bat.blah')),
+            Column('bar', Integer(), ForeignKey('bat.blah'), primary_key=True, key='bar'),
+        ]:
+            c2 = col.copy()
+            for attr in ('name', 'type', 'nullable', 'primary_key', 'key'):
+                eq_(getattr(col, attr), getattr(c2, attr))
+            eq_(len(col.args), len(c2.args))
+            for a1, a2 in zip(col.args, c2.args):
+                if isinstance(a1, ForeignKey):
+                    assert a2._colspec == 'bat.blah'
+                elif isinstance(a1, Sequence):
+                    assert a2.name == 'foo_seq'
+            
     def test_dupe_tables(self):
         metadata = MetaData()
         t1 = Table('table1', metadata, Column('col1', Integer, primary_key=True),