summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/sqlalchemy/dialects/mssql/base.py31
-rw-r--r--test/dialect/mssql/test_compiler.py12
2 files changed, 39 insertions, 4 deletions
diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py
index 051efa719..be8681178 100644
--- a/lib/sqlalchemy/dialects/mssql/base.py
+++ b/lib/sqlalchemy/dialects/mssql/base.py
@@ -386,7 +386,26 @@ Similarly, we can generate a clustered unique constraint using::
UniqueConstraint("y", mssql_clustered=True),
)
- .. versionadded:: 0.9.2
+.. 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 used to default to
+ False, now it defaults to None. ``mssql_clustered=False`` now explicitly
+ requests a non-clustered primary key and None relies on default SQL Server
+ behavior.
+
MSSQL-Specific Index Options
-----------------------------
@@ -1460,8 +1479,12 @@ class MSDDLCompiler(compiler.DDLCompiler):
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)
@@ -1572,7 +1595,7 @@ class MSDialect(default.DefaultDialect):
construct_arguments = [
(sa_schema.PrimaryKeyConstraint, {
- "clustered": False
+ "clustered": None
}),
(sa_schema.UniqueConstraint, {
"clustered": False
diff --git a/test/dialect/mssql/test_compiler.py b/test/dialect/mssql/test_compiler.py
index b59ca4fd1..2d5e986d3 100644
--- a/test/dialect/mssql/test_compiler.py
+++ b/test/dialect/mssql/test_compiler.py
@@ -625,6 +625,18 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
"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_uc_clustering(self):
metadata = MetaData()
tbl = Table('test', metadata,