Fix duplicate clustered index issue when both CreateWithPrimaryKey and UniqueIndexName are set#2160
Merged
Conversation
Co-authored-by: jzabroski <[email protected]>
Co-authored-by: jzabroski <[email protected]>
Copilot
AI
changed the title
[WIP] Issue: Duplicate Clustered Index on Version Table Causes Migration Failure
Fix duplicate clustered index issue when both CreateWithPrimaryKey and UniqueIndexName are set
Sep 6, 2025
jzabroski
requested changes
Sep 6, 2025
Comment on lines
+144
to
+152
| if (!_versionTableMeta.CreateWithPrimaryKey) | ||
| { | ||
| Create.Index(_versionTableMeta.UniqueIndexName) | ||
| .OnTable(_versionTableMeta.TableName) | ||
| .InSchema(_versionTableMeta.SchemaName) | ||
| .WithOptions().Unique() | ||
| .WithOptions().Clustered() | ||
| .OnColumn(_versionTableMeta.ColumnName); | ||
| } |
Collaborator
There was a problem hiding this comment.
try this idea:
var createIndexBuilder = Create.Index(_versionTableMeta.UniqueIndexName)
.OnTable(_versionTableMeta.TableName)
.InSchema(_versionTableMeta.SchemaName);
if (!_versionTableMeta.CreateWithPrimaryKey)
{
createIndexBuilder
.WithOptions()
.Clustered();
}
else
{
createIndexBuilder
.WithOptions()
.NonClustered();
}
Contributor
Author
There was a problem hiding this comment.
Implemented your suggestion in commit a9c5b3e. The code now creates a non-clustered unique index when CreateWithPrimaryKey=true instead of skipping the index creation entirely.
Collaborator
|
@copilot Update this PR to help trigger a new GitHub Action workflow for this PR so that we can validate all the tests pass. |
Instead of skipping the unique index creation entirely when CreateWithPrimaryKey=true, now creates a non-clustered unique index. This maintains the UniqueIndexName functionality while avoiding SQL Server's "Cannot create more than one clustered index" error. When CreateWithPrimaryKey=false: Creates clustered unique index (existing behavior) When CreateWithPrimaryKey=true: Creates non-clustered unique index (new behavior) Co-authored-by: jzabroski <[email protected]>
Contributor
Author
jzabroski
marked this pull request as ready for review
November 11, 2025 15:31
This was referenced Dec 2, 2025
This was referenced Jan 30, 2026
This was referenced Feb 9, 2026
This was referenced Mar 6, 2026
This was referenced Mar 21, 2026
This was referenced Jun 8, 2026
This was referenced Jun 15, 2026
This was referenced Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using a custom
IVersionTableMetaDataimplementation with bothCreateWithPrimaryKey = trueandUniqueIndexNamespecified, FluentMigrator would attempt to create two clustered indexes on the same table, causing SQL Server to fail with:This occurred because:
VersionMigration.Up()creates a clustered primary key whenCreateWithPrimaryKey = trueVersionUniqueMigration.Up()unconditionally creates a unique clustered index usingUniqueIndexNameThe fix modifies
VersionUniqueMigration.Up()to create different index types based on theCreateWithPrimaryKeysetting:CreateWithPrimaryKey = false: Creates a clustered unique indexCreateWithPrimaryKey = true: Creates a non-clustered unique indexThis approach avoids the clustered index conflict while still maintaining the
UniqueIndexNamefunctionality in both scenarios.Example scenario that now works:
The change maintains backward compatibility:
CreateWithPrimaryKey = falseandUniqueIndexNameis set → creates unique clustered index (existing behavior)CreateWithPrimaryKey = trueandUniqueIndexNameis set → creates non-clustered unique index (new behavior)Fixes #2051.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.