From 82d194c9a65b09fef8d52318cbe38e2c84dfd2ca Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 15 Apr 2010 19:05:41 -0400 Subject: - Added get_pk_constraint() to reflection.Inspector, similar to get_primary_keys() except returns a dict that includes the name of the constraint, for supported backends (PG so far). [ticket:1769] - Postgresql reflects the name of primary key constraints, if one exists. [ticket:1769] --- lib/sqlalchemy/engine/reflection.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'lib/sqlalchemy/engine/reflection.py') diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py index 57f2205c1..56b9eafd8 100644 --- a/lib/sqlalchemy/engine/reflection.py +++ b/lib/sqlalchemy/engine/reflection.py @@ -189,6 +189,26 @@ class Inspector(object): return pkeys + def get_pk_constraint(self, table_name, schema=None, **kw): + """Return information about primary key constraint on `table_name`. + + Given a string `table_name`, and an optional string `schema`, return + primary key information as a dictionary with these keys: + + constrained_columns + a list of column names that make up the primary key + + name + optional name of the primary key constraint. + + """ + pkeys = self.dialect.get_pk_constraint(self.conn, table_name, schema, + info_cache=self.info_cache, + **kw) + + return pkeys + + def get_foreign_keys(self, table_name, schema=None, **kw): """Return information about foreign_keys in `table_name`. @@ -208,6 +228,9 @@ class Inspector(object): a list of column names in the referred table that correspond to constrained_columns + name + optional name of the foreign key constraint. + \**kw other options passed to the dialect's get_foreign_keys() method. @@ -318,12 +341,14 @@ class Inspector(object): raise exc.NoSuchTableError(table.name) # Primary keys - primary_key_constraint = sa_schema.PrimaryKeyConstraint(*[ - table.c[pk] for pk in self.get_primary_keys(table_name, schema, **tblkw) - if pk in table.c - ]) - - table.append_constraint(primary_key_constraint) + pk_cons = self.get_pk_constraint(table_name, schema, **tblkw) + if pk_cons: + primary_key_constraint = sa_schema.PrimaryKeyConstraint(*[ + table.c[pk] for pk in pk_cons['constrained_columns'] + if pk in table.c + ], name=pk_cons.get('name')) + + table.append_constraint(primary_key_constraint) # Foreign keys fkeys = self.get_foreign_keys(table_name, schema, **tblkw) -- cgit v1.2.1