]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- fixes to tometadata() operation to propigate Constraints at column and table level
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 17 Feb 2007 19:08:36 +0000 (19:08 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 17 Feb 2007 19:08:36 +0000 (19:08 +0000)
CHANGES
lib/sqlalchemy/schema.py
test/engine/reflection.py

diff --git a/CHANGES b/CHANGES
index 612597f1157d86c7501622ce9f94657d71cff2db..c2e16c3c7867ebf0f3612e51f0a6d0cac03bc540 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -14,6 +14,7 @@
   - small fix to BoundMetaData to accept unicode or string URLs
   - fixed named PrimaryKeyConstraint generation [ticket:466] courtesy andrija at gmail
   - fixed generation of CHECK constraints on columns [ticket:464]
+  - fixes to tometadata() operation to propigate Constraints at column and table level
 - orm:
   - another refactoring to relationship calculation.  Allows more accurate ORM behavior
   with relationships from/to/between mappers, particularly polymorphic mappers,
index d0863800b95bb4e45e8a32495d1e89516158c286..debde5754e624f791655c0c24b1443df8971f587 100644 (file)
@@ -459,7 +459,7 @@ class Column(SchemaItem, sql._ColumnClause):
 
     def copy(self): 
         """creates a copy of this Column, unitialized.  this is used in Table.tometadata."""
-        return Column(self.name, self.type, self.default, key = self.key, primary_key = self.primary_key, nullable = self.nullable, _is_oid = self._is_oid, case_sensitive=self._case_sensitive_setting, quote=self.quote)
+        return Column(self.name, self.type, self.default, key = self.key, primary_key = self.primary_key, nullable = self.nullable, _is_oid = self._is_oid, case_sensitive=self._case_sensitive_setting, quote=self.quote, *[c.copy() for c in self.constraints])
         
     def _make_proxy(self, selectable, name = None):
         """create a "proxy" for this column.
@@ -712,7 +712,9 @@ class CheckConstraint(Constraint):
     def _set_parent(self, parent):
         self.parent = parent
         parent.constraints.add(self)
-                
+    def copy(self):
+        return CheckConstraint(self.sqltext, name=self.name)
+                    
 class ForeignKeyConstraint(Constraint):
     """table-level foreign key constraint, represents a colleciton of ForeignKey objects."""
     def __init__(self, columns, refcolumns, name=None, onupdate=None, ondelete=None, use_alter=False):
@@ -779,7 +781,9 @@ class UniqueConstraint(Constraint):
         self.columns.add(col)
     def accept_schema_visitor(self, visitor, traverse=True):
         visitor.visit_unique_constraint(self)
-        
+    def copy(self):
+        return UniqueConstraint(name=self.name, *self.__colnames)
+            
 class Index(SchemaItem):
     """Represents an index of columns from a database table
     """
index 7575e7a5daae724e4be3f6168bf9616bb8a65e74..388ed30c80d848d7bb04e5eae9e540594baf2f8e 100644 (file)
@@ -355,14 +355,15 @@ class ReflectionTest(PersistTest):
         finally:
             meta.drop_all()
             
-    def testtoengine(self):
+    def testtometadata(self):
         meta = MetaData('md1')
         meta2 = MetaData('md2')
         
         table = Table('mytable', meta,
             Column('myid', Integer, primary_key=True),
             Column('name', String, nullable=False),
-            Column('description', String(30)),
+            Column('description', String(30), CheckConstraint("description='hi'")),
+            UniqueConstraint('name')
         )
         
         table2 = Table('othertable', meta,
@@ -382,6 +383,20 @@ class ReflectionTest(PersistTest):
         assert [x.name for x in table.primary_key] == [x.name for x in table_c.primary_key]
         assert list(table2_c.c.myid.foreign_keys)[0].column is table_c.c.myid
         assert list(table2_c.c.myid.foreign_keys)[0].column is not table.c.myid
+        for c in table_c.c.description.constraints:
+            if isinstance(c, CheckConstraint):
+                break
+        else:
+            assert False
+        assert c.sqltext=="description='hi'"
+        
+        for c in table_c.constraints:
+            if isinstance(c, UniqueConstraint):
+                break
+        else:
+            assert False
+        assert c.columns.contains_column(table_c.c.name)
+        assert not c.columns.contains_column(table.c.name)
         
     # mysql throws its own exception for no such table, resulting in 
     # a sqlalchemy.SQLError instead of sqlalchemy.NoSuchTableError.