diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-01-20 17:55:01 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-01-20 18:06:18 -0500 |
commit | 49f1807f8f2acea5494fa77d217dce813a933147 (patch) | |
tree | 1d0e96e90604ee24ff250fd57b3a73d1d2b209ec /lib/sqlalchemy/engine/reflection.py | |
parent | 44cba66cb2eb4411b43c228c4f1191c764607855 (diff) | |
download | sqlalchemy-49f1807f8f2acea5494fa77d217dce813a933147.tar.gz |
- simplify the mechanics of PrimaryKeyConstraint with regards to reflection;
reflection now updates the PKC in place.
- support the use case of the empty PrimaryKeyConstraint in order to specify
constraint options; the columns marked as primary_key=True will now be gathered
into the columns collection, rather than being ignored. [ticket:2910]
- add validation such that column specification should only take place
in the PrimaryKeyConstraint directly, or by using primary_key=True flags;
if both are present, they have to match exactly, otherwise the condition is
assumed to be ambiguous, and a warning is emitted; the old behavior of
using the PKC columns only is maintained.
Diffstat (limited to 'lib/sqlalchemy/engine/reflection.py')
-rw-r--r-- | lib/sqlalchemy/engine/reflection.py | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py index d82aac7fd..9e6cf61dc 100644 --- a/lib/sqlalchemy/engine/reflection.py +++ b/lib/sqlalchemy/engine/reflection.py @@ -504,6 +504,8 @@ class Inspector(object): cols_by_orig_name[orig_name] = col = \ sa_schema.Column(name, coltype, *colargs, **col_kw) + if col.key in table.primary_key: + col.primary_key = True table.append_column(col) if not found_table: @@ -516,17 +518,20 @@ class Inspector(object): for pk in pk_cons['constrained_columns'] if pk in cols_by_orig_name and pk not in exclude_columns ] - pk_cols += [ - pk - for pk in table.primary_key - if pk.key in exclude_columns - ] - primary_key_constraint = sa_schema.PrimaryKeyConstraint( - name=pk_cons.get('name'), - *pk_cols - ) - table.append_constraint(primary_key_constraint) + # 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() + fkeys = self.get_foreign_keys(table_name, schema, **table.dialect_kwargs) for fkey_d in fkeys: |