# 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:
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.
: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):