Skip to content

Simplify SQL Server DROP DEFAULT CONSTRAINT migration query - #38250

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
m-x-shokhzod:perf/sqlserver-simpler-drop-default-constraint
Jun 9, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
m-x-shokhzod:perf/sqlserver-simpler-drop-default-constraint

Conversation

@m-x-shokhzod

Copy link
Copy Markdown
Contributor

Replaces the JOIN-based lookup against [sys].[default_constraints] + [sys].[columns] with a single-table query against [sys].[columns] using OBJECT_NAME([c].[default_object_id]). Tightens the variable type from nvarchar(max) to sysname.

  • One sys-table reference instead of two; no JOIN
  • Variable type matches what SQL Server returns for object names (sysname = nvarchar(128) NOT NULL)
  • Behavior preserved: when no default constraint exists, OBJECT_NAME returns NULL and the existing IF @var IS NOT NULL EXEC(...) guard skips the drop

Before:

DECLARE @var nvarchar(max);
SELECT @var = QUOTENAME([d].[name])
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[People]') AND [c].[name] = N'LuckyNumber');
IF @var IS NOT NULL EXEC(N'ALTER TABLE [People] DROP CONSTRAINT ' + @var + ';');

After:

DECLARE @var sysname;
SELECT @var = QUOTENAME(OBJECT_NAME([c].[default_object_id]))
FROM [sys].[columns] [c]
WHERE [c].[object_id] = OBJECT_ID(N'[People]') AND [c].[name] = N'LuckyNumber';
IF @var IS NOT NULL EXEC(N'ALTER TABLE [People] DROP CONSTRAINT ' + @var + ';');

Test expectations updated across MigrationsSqlServerTest, MigrationsSqlServerTest.TemporalTables, MigrationsSqlServerTest.NamedDefaultConstraints, and SqlServerMigrationsSqlGeneratorTest (162 occurrences). End-to-end execution verified by the MigrationsSqlServerTest* functional suites against real SQL Server in the CI matrix (2019/2022/2025).

Fixes #34620

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request simplifies the SQL Server migrations SQL used to drop a default constraint by switching from a sys.default_constraints+sys.columns join to a single lookup on sys.columns via OBJECT_NAME([c].[default_object_id]), and updates SQL assertion expectations accordingly (Fixes #34620).

Changes:

  • Updated SqlServerMigrationsSqlGenerator.DropDefaultConstraint to query sys.columns only and to declare the variable as sysname.
  • Updated functional test SQL baselines across multiple migration test suites to match the new generated SQL.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Changes default-constraint drop SQL generation to use sys.columns.default_object_id + OBJECT_NAME.
test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs Updates expected SQL strings for migration SQL generator tests.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs Updates expected executed SQL for migration functional tests.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.TemporalTables.cs Updates expected executed SQL for temporal-table migration functional tests.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.NamedDefaultConstraints.cs Updates expected executed SQL for named default-constraint scenarios.

m-x-shokhzod added a commit to m-x-shokhzod/efcore that referenced this pull request May 31, 2026
Addresses Copilot review on dotnet#38250: the variable was changed from
nvarchar(max) to sysname, but the SELECT still stores QUOTENAME(...)
whose output can reach 258 chars and would silently truncate inside a
128-char sysname, producing invalid DROP CONSTRAINT SQL for long names.

The variable-type change was never part of issue dotnet#34620, which only asks
to replace the sys.default_constraints JOIN with a single-table lookup
via OBJECT_NAME([c].[default_object_id]). QUOTENAME is required here by
dotnet#36271 (use QUOTENAME when constructing SQL for EXEC), so the matching
nvarchar(max) must stay. Revert the variable to nvarchar(max) and keep
only the lookup simplification.

Restore the nvarchar(max) baseline in all affected test expectations.
@AndriySvyryd

Copy link
Copy Markdown
Member

@m-x-shokhzod Please rebase on latest main

Replace the JOIN against [sys].[default_constraints] + [sys].[columns]
with a single-table lookup on [sys].[columns] via
OBJECT_NAME([c].[default_object_id]). One sys-table, no JOIN; behavior is
preserved (OBJECT_NAME returns NULL when there is no default constraint,
so the existing IF @var IS NOT NULL guard still skips the drop).

The nvarchar(max) variable and QUOTENAME wrapping are kept: QUOTENAME's
output can exceed sysname's 128 chars, and QUOTENAME is required when
constructing the dynamic SQL passed to EXEC (dotnet#36271).

Fixes dotnet#34620
@m-x-shokhzod
m-x-shokhzod force-pushed the perf/sqlserver-simpler-drop-default-constraint branch from 9d617a9 to be05b4b Compare June 9, 2026 10:56

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit f095ae4 into dotnet:main Jun 9, 2026
13 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlServer Migrations: Simplify drop default constraint query

3 participants