From 30242df2a832382ad1eb7342318efe190c6070e9 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Thu, 18 Feb 2021 18:57:02 +0100 Subject: [PATCH] Fix a reflection error for MSSQL 2005 The reflection of filtered indexes broke index reflection error for MSSQL 2005 that does not support that them Fixes #5930 Change-Id: I5d1f4fa8ba5bca31e91981076e4ee476ddfba49a --- doc/build/changelog/unreleased_14/5930.rst | 6 ++++++ lib/sqlalchemy/dialects/mssql/base.py | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_14/5930.rst diff --git a/doc/build/changelog/unreleased_14/5930.rst b/doc/build/changelog/unreleased_14/5930.rst new file mode 100644 index 0000000000..7751a2b616 --- /dev/null +++ b/doc/build/changelog/unreleased_14/5930.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, mssql + :tickets: 5919 + + Fix a reflection error for MSSQL 2005 introduced by the reflection of + filtered indexes. diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index a0aa67c69a..81f66e2f09 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -2894,16 +2894,22 @@ class MSDialect(default.DefaultDialect): @reflection.cache @_db_plus_owner def get_indexes(self, connection, tablename, dbname, owner, schema, **kw): + filter_definition = ( + "ind.filter_definition" + if self.server_version_info >= MS_2008_VERSION + else "NULL as filter_definition" + ) rp = connection.execution_options(future_result=True).execute( sql.text( "select ind.index_id, ind.is_unique, ind.name, " - "ind.filter_definition " + "%s " "from sys.indexes as ind join sys.tables as tab on " "ind.object_id=tab.object_id " "join sys.schemas as sch on sch.schema_id=tab.schema_id " "where tab.name = :tabname " "and sch.name=:schname " "and ind.is_primary_key=0 and ind.type != 0" + % filter_definition ) .bindparams( sql.bindparam("tabname", tablename, ischema.CoerceUnicode()), -- 2.47.2