From: Gord Thompson Date: Tue, 8 Dec 2020 20:23:02 +0000 (-0500) Subject: Fixed compile for mssql dialect X-Git-Tag: rel_1_3_21~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e0a8dbdb22bf545ae9eee433d88336821fcd8df;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fixed compile for mssql dialect Fixed string compilation when both mssql_include and mssql_where are used Fixes: #5751 Closes: #5752 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5752 Pull-request-sha: aa57ad5d93cd69bf7728d864569c31c7e59b54fb Change-Id: I1201170affd9911c252df5c9b841e538bb577085 (cherry picked from commit 6f8b035c7808fea13bae50995206c9b2ded91f51) --- diff --git a/doc/build/changelog/unreleased_13/5751.rst b/doc/build/changelog/unreleased_13/5751.rst new file mode 100644 index 0000000000..44306ceb3d --- /dev/null +++ b/doc/build/changelog/unreleased_13/5751.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, mssql + :tickets: 5751 + + Fixed bug where a CREATE INDEX statement was rendered incorrectly when + both ``mssql-include`` and ``mssql_where`` were specified. Pull request + courtesy @Adiorz. diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 6869e504ad..cb5d677a18 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -2043,14 +2043,6 @@ class MSDDLCompiler(compiler.DDLCompiler): ), ) - whereclause = index.dialect_options["mssql"]["where"] - - if whereclause is not None: - 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 = [ @@ -2064,6 +2056,14 @@ class MSDDLCompiler(compiler.DDLCompiler): [preparer.quote(c.name) for c in inclusions] ) + whereclause = index.dialect_options["mssql"]["where"] + + if whereclause is not None: + 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 34fd20076d..e0d606c771 100644 --- a/test/dialect/mssql/test_compiler.py +++ b/test/dialect/mssql/test_compiler.py @@ -18,6 +18,7 @@ from sqlalchemy import sql from sqlalchemy import String from sqlalchemy import Table from sqlalchemy import testing +from sqlalchemy import text from sqlalchemy import union from sqlalchemy import UniqueConstraint from sqlalchemy import update @@ -1255,6 +1256,31 @@ 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=text("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))