microsoft / microsoft/vscode-mssql
[Bug]: SQL Database Project: duplicate creation of constraints
- Dominant language
- TypeScript
- Stars
- 1.9k
- Forks
- 610
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 97
Description
### Description
Hi, I recently started using SQL Database Project on VS-Code. I'm having a problem with the code that creates the tables. I'll give a practical example, sharing a T-SQL script that's generated after running a build.
```sql
CREATE TABLE [dbo].[MyTable01] (
[IdField01] INT IDENTITY (1, 1) NOT NULL,
[Field02] INT NOT NULL,
[Field03] SMALLINT NOT NULL,
[Field04] VARCHAR (10) NULL,
CONSTRAINT [PK_MyTable01] PRIMARY KEY CLUSTERED ([IdField01] ASC),
CONSTRAINT [FK_MyTable01_MyTable02] FOREIGN KEY ([Field03]) REFERENCES [dbo].[MyTable02] ([Column01]),
CONSTRAINT [FK_MyTable01_MyTable03] FOREIGN KEY ([Field02]) REFERENCES [dbo].[MyTable03] ([Field02])
);
GO
ALTER TABLE [dbo].[MyTable01]
ADD CONSTRAINT [PK_MyTable01] PRIMARY KEY CLUSTERED ([IdField01] ASC);
GO
ALTER TABLE [dbo].[MyTable01]
ADD CONSTRAINT [FK_MyTable01_MyTable02] FOREIGN KEY ([Field03]) REFERENCES [dbo].[MyTable02] ([Column01]);
GO
ALTER TABLE [dbo].[MyTable01]
ADD CONSTRAINT [FK_MyTable01_MyTable03] FOREIGN KEY ([Field02]) REFERENCES [dbo].[MyTable03] ([Field02]);
GO
CREATE NONCLUSTERED INDEX [IX_MyTable01_Field02]
ON [dbo].[MyTable01]([Field02] ASC);
GO
CREATE UNIQUE NONCLUSTERED INDEX [UIX_MyTable01_Gestione_EsitoPerizia]
ON [dbo].[MyTable01]([Field02] ASC, [Field03] ASC);
GO
```
The problem I'm encountering is that the following objects:
PK_MyTable01
FK_MyTable01_MyTable02
FK_MyTable01_MyTable03
are duplicated, which causes build errors. Since I have about 1,000 tables with this problem, is there a solution that doesn't require manually deleting the redundant code?
### Steps to Reproduce
1. Right click on the database project name
2. Update Project from Database
3. Select SQL Server Instance
4. Select Database
5. Apply all changes
6. Confirm you want to update target project
### Affected Area
- [ ] Connection dialog (SQL Server | Azure browse/Fabric browse)
- [ ] Query editor
- [ ] Query results panel
- [ ] Object Explorer
- [ ] GitHub Copilot integration
- [ ] Preview/Edit data
- [ ] Table Designer
- [ ] Schema Designer
- [ ] Schema Compare
- [ ] Local SQL Server Container provisioning
- [ ] SQL database in Fabric provisioning
- [ ] DACPAC/BACPAC export/import
- [x] SQL Database projects
- [ ] Query Plan Visualizer
- [ ] Other (please describe below)
### If you selected "Other", please describe the affected area
_No response_
### Environment Information
- VS Code:
Version: 1.108.2 (system setup)
Commit: c9d77990917f3102ada88be140d28b038d1dd7c7
Date: 2026-01-21T13:52:09.270Z
Electron: 39.2.7
ElectronBuildId: 12953945
Chromium: 142.0.7444.235
Node.js: 22.21.1
V8: 14.2.231.21-electron.0
OS: Windows_NT x64 10.0.26100
- SQL Server: Microsoft SQL Server 2019 (RTM-CU32-GDR) (KB5065222) - 15.0.4445.1 (X64) Aug 13 2025 11:07:18 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2019 Datacenter 10.0 (Build 17763: ) (Hypervisor)
- SQL Server Database Project: 1.5.6
### Confirmation
- [x] I have searched existing issues and couldn't find a match
- [ ] I want to work on this issue
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the issue through Update Project from Database using the listed SQL Server and SQL Database Projects versions. Compare the generated CREATE TABLE and ALTER TABLE sections, then verify that each primary and foreign key is emitted once and the resulting project builds without duplicate-constraint errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sql, vscode
- Domain
- databases, developer-experience
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100