DacFx icon indicating copy to clipboard operation
DacFx copied to clipboard

Can't import a bacpac with sqlpackage if it contains default constraints in mixed styles

Open eberlekhaufe opened this issue 5 months ago • 1 comments

  • SqlPackage or DacFx Version: 162.2.111.2
  • .NET Framework (Windows-only) or .NET Core: 10.0.9
  • Environment (local platform and source/target platforms): Windows x64 / Azure Devops Pipeline

We maintain a bacpac used for our automated tests. After a recent change to the content of the bacpac we started to see the error when we tried to import the bacpac:

*** Error importing database:Could not import package.
Error SQL72014: Core Microsoft SqlClient Data Provider: Msg 2714, Level 16, State 5, Line 1 There is already an object named 'DF__AUSchedul__Compa__44ABD28D' in the database.
Error SQL72045: Script execution error.  The executed script:
ALTER TABLE [dbo].[AUScheduleFill]
    ADD CONSTRAINT [DF__AUSchedul__Compa__44ABD28D] DEFAULT (0xAAAAAAAA) FOR [CompanyMask];


Error SQL72014: Core Microsoft SqlClient Data Provider: Msg 1750, Level 16, State 1, Line 1 Could not create constraint or index. See previous errors.
Error SQL72045: Script execution error.  The executed script:
ALTER TABLE [dbo].[AUScheduleFill]
    ADD CONSTRAINT [DF__AUSchedul__Compa__44ABD28D] DEFAULT (0xAAAAAAAA) FOR [CompanyMask];

I analyzed the failing bacpac (model.xml) and found that there is no duplicate default constraint in the bacpac at all. I then stumbled across this page: https://learn.microsoft.com/en-us/sql/tools/sqlpackage/troubleshooting-issues-and-performance-with-sqlpackage?view=sql-server-ver17#import-action-error-2714-for-constraint

And it seems that we really ran into the issue described there. For some reason sqlpackage doesn't cope with default constraints defined in different styles. So no duplicate constraint at all.

The only workaround I was able to find is to unpack the bacpac, unify the default constraint definitions in the bacpac (< 200 items) and repackage it again. As this has to happen in a pipeline, it is kind of a error-prone workaround.

So we would appreciate a lot if SqlPackage could be fixed to support mixed styles of default constraints.

Steps to Reproduce: Disclaimer: We do change our bacpac a lot. And the mixed style constraints where there before, and the sqlpackage import has always worked. Only with the latest change (which is too big to analyze) this started to happen. So we don't really know what exactly caused it. Furthermore our bacpac is 700MB big and can't be shared here I think, also for legal reasons.

Nonetheless I can outline the steps which lead to the bug, assuming such a bacpac

  1. DB uses mixed styles of default constraints
  2. Do some changes to the DB (this is the big unknown)
  3. Export the DB with sqlpackage to a bacpac
  4. Import the bacpac again. Error from above is shown

Did this occur in prior versions? If not - which version(s) did it work in?

(DacFx/SqlPackage/SSMS/Azure Data Studio)

eberlekhaufe avatar Jun 11 '26 11:06 eberlekhaufe

@eberlekhaufe we tried to repro the bug with no success; to diagnose further without the full 700 MB database, we will need your help to get:

  • The model.xml extracted from the failing .bacpac (a .bacpac is a zip; model.xml is schema-only, no data) — the single most useful artifact; we can feed it straight into an import and reproduce deterministically.
  • The two ALTER TABLE … ADD CONSTRAINT statements that produced the 2714 from the full diagnostics log (/DiagnosticsFile: /DiagnosticsLevel:Verbose), so we can see exactly which constraint is emitted twice.
  • If model.xml can't be shared as-is, the definition of AUScheduleFill and any other tables sharing the DF__AUSchedul__Compa__… name fragment, plus an approximate count of default constraints in the database.

allancascante avatar Jun 25 '26 17:06 allancascante