.. changelog::
:version: 1.1.0b1
+ .. change::
+ :tags: feature, mssql
+
+ The ``mssql_clustered`` flag available on :class:`.UniqueConstraint`,
+ :class:`.PrimaryKeyConstraint`, :class:`.Index` now defaults to
+ ``None``, and can be set to False which will render the NONCLUSTERED
+ keyword in particular for a primary key, allowing a different index to
+ be used as "clustered". Pull request courtesy Saulius Žemaitaitis.
+
.. change::
:tags: feature, orm
:tickets: 1311
:ticket:`3504`
+Support for "non clustered" on primary key to allow clustered elsewhere
+-----------------------------------------------------------------------
+
+The ``mssql_clustered`` flag available on :class:`.UniqueConstraint`,
+:class:`.PrimaryKeyConstraint`, :class:`.Index` now defaults to ``None``, and
+can be set to False which will render the NONCLUSTERED keyword in particular
+for a primary key, allowing a different index to be used as "clustered".
+
+.. seealso::
+
+ :ref:`mssql_indexes`
+
.. _change_3434:
The legacy_schema_aliasing flag is now set to False
which renders the index as ``CREATE CLUSTERED INDEX my_index ON table (x)``.
-.. versionadded:: 0.8
-
To generate a clustered primary key use::
Table('my_table', metadata,
UniqueConstraint("y", mssql_clustered=True),
)
- .. versionadded:: 0.9.2
+To explicitly request a non-clustered primary key (for example, when
+a separate clustered index is desired), use::
+
+ Table('my_table', metadata,
+ Column('x', ...),
+ Column('y', ...),
+ PrimaryKeyConstraint("x", "y", mssql_clustered=False))
+
+which will render the table, for example, as::
+
+ CREATE TABLE my_table (x INTEGER NOT NULL, y INTEGER NOT NULL,
+ PRIMARY KEY NONCLUSTERED (x, y))
+
+.. versionchanged:: 1.1 the ``mssql_clustered`` option now defaults
+ to None, rather than False. ``mssql_clustered=False`` now explicitly
+ renders the NONCLUSTERED clause, whereas None omits the CLUSTERED
+ clause entirely, allowing SQL Server defaults to take effect.
+
MSSQL-Specific Index Options
-----------------------------
text += "UNIQUE "
# handle clustering option
- if index.dialect_options['mssql']['clustered']:
- text += "CLUSTERED "
+ clustered = index.dialect_options['mssql']['clustered']
+ if clustered is not None:
+ if clustered:
+ text += "CLUSTERED "
+ else:
+ text += "NONCLUSTERED "
text += "INDEX %s ON %s (%s)" \
% (
self.preparer.format_constraint(constraint)
text += "PRIMARY KEY "
- if constraint.dialect_options['mssql']['clustered']:
- text += "CLUSTERED "
+ clustered = constraint.dialect_options['mssql']['clustered']
+ if clustered is not None:
+ if clustered:
+ text += "CLUSTERED "
+ else:
+ text += "NONCLUSTERED "
text += "(%s)" % ', '.join(self.preparer.quote(c.name)
for c in constraint)
self.preparer.format_constraint(constraint)
text += "UNIQUE "
- if constraint.dialect_options['mssql']['clustered']:
- text += "CLUSTERED "
+ clustered = constraint.dialect_options['mssql']['clustered']
+ if clustered is not None:
+ if clustered:
+ text += "CLUSTERED "
+ else:
+ text += "NONCLUSTERED "
text += "(%s)" % ', '.join(self.preparer.quote(c.name)
for c in constraint)
construct_arguments = [
(sa_schema.PrimaryKeyConstraint, {
- "clustered": False
+ "clustered": None
}),
(sa_schema.UniqueConstraint, {
- "clustered": False
+ "clustered": None
}),
(sa_schema.Index, {
- "clustered": False,
+ "clustered": None,
"include": None
})
]
"PRIMARY KEY CLUSTERED (x, y))"
)
+ def test_table_pkc_explicit_nonclustered(self):
+ metadata = MetaData()
+ tbl = Table('test', metadata,
+ Column('x', Integer, autoincrement=False),
+ Column('y', Integer, autoincrement=False),
+ PrimaryKeyConstraint("x", "y", mssql_clustered=False))
+ self.assert_compile(
+ schema.CreateTable(tbl),
+ "CREATE TABLE test (x INTEGER NOT NULL, y INTEGER NOT NULL, "
+ "PRIMARY KEY NONCLUSTERED (x, y))"
+ )
+
+ def test_table_idx_explicit_nonclustered(self):
+ metadata = MetaData()
+ tbl = Table(
+ 'test', metadata,
+ Column('x', Integer, autoincrement=False),
+ Column('y', Integer, autoincrement=False)
+ )
+
+ idx = Index("myidx", tbl.c.x, tbl.c.y, mssql_clustered=False)
+ self.assert_compile(
+ schema.CreateIndex(idx),
+ "CREATE NONCLUSTERED INDEX myidx ON test (x, y)"
+ )
+
+ def test_table_uc_explicit_nonclustered(self):
+ metadata = MetaData()
+ tbl = Table('test', metadata,
+ Column('x', Integer, autoincrement=False),
+ Column('y', Integer, autoincrement=False),
+ UniqueConstraint("x", "y", mssql_clustered=False))
+ self.assert_compile(
+ schema.CreateTable(tbl),
+ "CREATE TABLE test (x INTEGER NULL, y INTEGER NULL, "
+ "UNIQUE NONCLUSTERED (x, y))"
+ )
+
def test_table_uc_clustering(self):
metadata = MetaData()
tbl = Table('test', metadata,