Fixed issue where the index reflection for SQL Server would
not correctly return the order of the column inside an index
when the order of the columns in the index did not match the
order of the columns in the table.
Pull request courtesy of Allen Chen.
Fixes: #12894
Closes: #12895
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12895
Pull-request-sha:
bd9bd43219f35a29eaeee81fedea452afc64eb5d
Change-Id: I45ed30bbd0fcfd4f67cb2b682ecb3a18029be2b7
(cherry picked from commit
3dc9720c365a8d03e7c173874db74a080752d24e)
--- /dev/null
+.. change::
+ :tags: bug, mssql
+ :tickets: 12894
+
+ Fixed issue where the index reflection for SQL Server would
+ not correctly return the order of the column inside an index
+ when the order of the columns in the index did not match the
+ order of the columns in the table.
+ Pull request courtesy of Allen Chen.
where
tab.name = :tabname
and sch.name = :schname
+order by
+ ind_col.index_id,
+ ind_col.key_ordinal
"""
)
.bindparams(
ASC/DESC but reflects them as expressions (like oracle)."""
return exclusions.closed()
+ @property
+ def indexes_check_column_order(self):
+ """target database supports CREATE INDEX with column order check."""
+ return exclusions.closed()
+
@property
def indexes_with_expressions(self):
"""target database supports CREATE INDEX against SQL expressions."""
],
)
+ @testing.requires.indexes_check_column_order
+ def test_index_column_order(self, metadata, inspect_for_table):
+ """test for #12894"""
+ with inspect_for_table("sa_multi_index") as (schema, inspector):
+ test_table = Table(
+ "sa_multi_index",
+ metadata,
+ Column("Column1", Integer, primary_key=True),
+ Column("Column2", Integer),
+ Column("Column3", Integer),
+ )
+ Index(
+ "Index_Example",
+ test_table.c.Column3,
+ test_table.c.Column1,
+ test_table.c.Column2,
+ )
+ indexes = inspector.get_indexes("sa_multi_index")
+ eq_(indexes[0]["column_names"], ["Column3", "Column1", "Column2"])
+
@testing.requires.indexes_with_expressions
def test_reflect_expression_based_indexes(self, metadata, connection):
t = Table(
CreateIndex(idx), "CREATE NONCLUSTERED INDEX idx_x ON t (x)"
)
+ def test_index_column_order_clustered(self, metadata, connection):
+ """test for #12894"""
+ test_table = Table(
+ "t",
+ metadata,
+ Column("id", Integer, primary_key=True),
+ Column("x", Integer),
+ Column("y", Integer),
+ PrimaryKeyConstraint("id", mssql_clustered=False),
+ )
+ Index(
+ "idx_x",
+ test_table.c.y,
+ test_table.c.id,
+ test_table.c.x,
+ mssql_clustered=True,
+ )
+ metadata.create_all(connection)
+ indexes = testing.db.dialect.get_indexes(connection, "t", None)
+ eq_(indexes[0]["column_names"], ["y", "id", "x"])
+
@testing.only_if("mssql>=12")
def test_index_reflection_colstore_clustered(self, metadata, connection):
t1 = Table(
]
)
+ @property
+ def indexes_check_column_order(self):
+ return exclusions.open()
+
@property
def indexes_with_expressions(self):
return only_on(["postgresql", "sqlite>=3.9.0", "oracle"])