I have a Visual Studio sql project with a table defined like the following:
CREATE TABLE [dbo].[Hoerses]
(
[HoersId] INT NOT NULL PRIMARY KEY,
[DatePurchased] datetime NOT NULL CONSTRAINT [DF_Hoerses_DatePurchased] DEFAULT DATETIMEFROMPARTS(1985,01,01,0,0,0,0)
)
When I target a preexisting SQL database with a "Script" command
sqlpackage.exe /Action:Script /SourceFile:DatabaseProject1.dacpac /Profile:publish.xml /OutputPath:deployscript_test.sql /TargetPassword:redacted
Then I get the following generated SQL even though the constraint had the same name and definition before & after:
PRINT N'Dropping [dbo].[DF_Hoerses_DatePurchased]...';
GO
ALTER TABLE [dbo].[Hoerses] DROP CONSTRAINT [DF_Hoerses_DatePurchased];
GO
PRINT N'Creating [dbo].[DF_Hoerses_DatePurchased]...';
GO
ALTER TABLE [dbo].[Hoerses]
ADD CONSTRAINT [DF_Hoerses_DatePurchased] DEFAULT DATETIMEFROMPARTS(1985,01,01,0,0,0,0) FOR [DatePurchased];
GO
PRINT N'Update complete.';
GO
(My main concern with trying to prevent this superfluous re-creation is because I occasionally see a "Lock request time out period exceeded." error when it's trying to drop a constraint during actual deployments/publishing)
The problem was apparently in the use of DATETIMEFROMPARTS
.
If I instead declare the table as
CREATE TABLE [dbo].[Hoerses]
(
[HoersId] INT NOT NULL PRIMARY KEY,
[DatePurchased] datetime NOT NULL CONSTRAINT [DF_Hoerses_DatePurchased] DEFAULT '1985-01-01'
)
Then SqlPackage.exe no longer tries to drop & re-add the constraint.