From 607af882c35d0e37dbc2548e8cfb5b9b30df3032 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 10 Nov 2012 15:37:51 -0500 Subject: [PATCH] Support for reflection of the "name" of primary key constraints added, courtesy Dave Moore. mssql [ticket:2600] --- doc/build/changelog/changelog_08.rst | 7 +++++++ lib/sqlalchemy/dialects/mssql/base.py | 7 +++++-- test/requirements.py | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index e10780c83c..7f1b1d0fdc 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -16,6 +16,13 @@ since 0.7 doesn't respect the ".key" in a wider range of scenarios. + .. change:: + :tags: mssql, feature + :tickets: 2600 + + Support for reflection of the "name" of primary key + constraints added, courtesy Dave Moore. + .. changelog:: :version: 0.8.0b1 :released: October 30, 2012 diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 51884247f9..f9b7b944ce 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -1415,17 +1415,20 @@ class MSDialect(default.DefaultDialect): C = ischema.key_constraints.alias('C') # Primary key constraints - s = sql.select([C.c.column_name, TC.c.constraint_type], + s = sql.select([C.c.column_name, TC.c.constraint_type, C.c.constraint_name], sql.and_(TC.c.constraint_name == C.c.constraint_name, TC.c.table_schema == C.c.table_schema, C.c.table_name == tablename, C.c.table_schema == owner) ) c = connection.execute(s) + constraint_name = None for row in c: if 'PRIMARY' in row[TC.c.constraint_type.name]: pkeys.append(row[0]) - return {'constrained_columns': pkeys, 'name': None} + if constraint_name is None: + constraint_name = row[C.c.constraint_name.name] + return {'constrained_columns': pkeys, 'name': constraint_name} @reflection.cache @_db_plus_owner diff --git a/test/requirements.py b/test/requirements.py index b88b774322..736c001080 100644 --- a/test/requirements.py +++ b/test/requirements.py @@ -372,7 +372,7 @@ class DefaultRequirements(SuiteRequirements): def reflects_pk_names(self): """Target driver reflects the name of primary key constraints.""" - return fails_on_everything_except('postgresql', 'oracle') + return fails_on_everything_except('postgresql', 'oracle', 'mssql') @property def python2(self): -- 2.47.3