]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- further refine this so that the ordering of columns is maintained as
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 20 Jan 2014 23:14:02 +0000 (18:14 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 20 Jan 2014 23:14:02 +0000 (18:14 -0500)
sent to the primary key constraint; existing tests in the PG dialect
confirm this.

lib/sqlalchemy/engine/reflection.py
lib/sqlalchemy/sql/schema.py

index 9e6cf61dc5827e8a305d1fca292fbf7c208f4b9d..45f100518e90cb05b3b91310fab10762f045547c 100644 (file)
@@ -522,16 +522,9 @@ class Inspector(object):
             # update pk constraint name
             table.primary_key.name = pk_cons.get('name')
 
-            # set the primary key flag on new columns.
-            # note any existing PK cols on the table also have their
-            # flag still set.
-            for col in pk_cols:
-                col.primary_key = True
-
             # tell the PKConstraint to re-initialize
             # it's column collection
-            table.primary_key._reload()
-
+            table.primary_key._reload(pk_cols)
 
         fkeys = self.get_foreign_keys(table_name, schema, **table.dialect_kwargs)
         for fkey_d in fkeys:
index 09f52f8a720ea96be428a3c30ab3f76315528e8b..ba38b5070a43ac1cf2b526d82c2b367febf6d169 100644 (file)
@@ -2623,9 +2623,12 @@ class PrimaryKeyConstraint(ColumnCollectionConstraint):
             c.primary_key = True
         self.columns.extend(table_pks)
 
-    def _reload(self):
-        """repopulate this :class:`.PrimaryKeyConstraint` based on the current
-        columns marked with primary_key=True in the table.
+    def _reload(self, columns):
+        """repopulate this :class:`.PrimaryKeyConstraint` given
+        a set of columns.
+
+        Existing columns in the table that are marked as primary_key=True
+        are maintained.
 
         Also fires a new event.
 
@@ -2633,14 +2636,20 @@ class PrimaryKeyConstraint(ColumnCollectionConstraint):
         :class:`.PrimaryKeyConstraint` object on the parent
         :class:`.Table` object without actually replacing the object.
 
+        The ordering of the given list of columns is also maintained; these
+        columns will be appended to the list of columns after any which
+        are already present.
+
         """
 
-        # clear out the columns collection; we will re-populate
-        # based on current primary_key flags
-        self.columns.clear()
+        # set the primary key flag on new columns.
+        # note any existing PK cols on the table also have their
+        # flag still set.
+        for col in columns:
+            col.primary_key = True
+
+        self.columns.extend(columns)
 
-        # fire a new event; this will add all existing
-        # primary key columns based on the flag.
         self._set_parent_with_dispatch(self.table)
 
     def _replace(self, col):