From 4bc1c1b415f9a65d28b7eec8e190ec38a6901873 Mon Sep 17 00:00:00 2001 From: Adiorz Date: Mon, 7 Dec 2020 15:03:53 +0000 Subject: [PATCH] Fixed compile for mssql dialect Fixed string compilation when both mssql_include and mssql_where are used Fixes: #5751 --- lib/sqlalchemy/dialects/mssql/base.py | 24 ++++++++++++------------ test/dialect/mssql/test_compiler.py | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 9addbf31fd..911e1791ae 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -2286,18 +2286,6 @@ class MSDDLCompiler(compiler.DDLCompiler): ), ) - whereclause = index.dialect_options["mssql"]["where"] - - if whereclause is not None: - whereclause = coercions.expect( - roles.DDLExpressionRole, whereclause - ) - - where_compiled = self.sql_compiler.process( - whereclause, include_table=False, literal_binds=True - ) - text += " WHERE " + where_compiled - # handle other included columns if index.dialect_options["mssql"]["include"]: inclusions = [ @@ -2311,6 +2299,18 @@ class MSDDLCompiler(compiler.DDLCompiler): [preparer.quote(c.name) for c in inclusions] ) + whereclause = index.dialect_options["mssql"]["where"] + + if whereclause is not None: + whereclause = coercions.expect( + roles.DDLExpressionRole, whereclause + ) + + where_compiled = self.sql_compiler.process( + whereclause, include_table=False, literal_binds=True + ) + text += " WHERE " + where_compiled + return text def visit_drop_index(self, drop): diff --git a/test/dialect/mssql/test_compiler.py b/test/dialect/mssql/test_compiler.py index 568d346f5c..eea4011899 100644 --- a/test/dialect/mssql/test_compiler.py +++ b/test/dialect/mssql/test_compiler.py @@ -1281,6 +1281,29 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): schema.CreateIndex(idx), "CREATE INDEX foo ON test (x) INCLUDE (y)" ) + def test_index_include_where(self): + metadata = MetaData() + tbl = Table( + "test", + metadata, + Column("x", Integer), + Column("y", Integer), + Column("z", Integer), + ) + idx = Index("foo", tbl.c.x, mssql_include=[tbl.c.y], + mssql_where=tbl.c.y > 1) + self.assert_compile( + schema.CreateIndex(idx), + "CREATE INDEX foo ON test (x) INCLUDE (y) WHERE y > 1" + ) + + idx = Index("foo", tbl.c.x, mssql_include=[tbl.c.y], + mssql_where="y > 1") + self.assert_compile( + schema.CreateIndex(idx), + "CREATE INDEX foo ON test (x) INCLUDE (y) WHERE y > 1" + ) + def test_try_cast(self): metadata = MetaData() t1 = Table("t1", metadata, Column("id", Integer, primary_key=True)) -- 2.47.3