Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Foreign keys on multiple columns are not handled correctly #83

Open
lmeyer1 opened this issue May 9, 2019 · 1 comment
Open

Foreign keys on multiple columns are not handled correctly #83

lmeyer1 opened this issue May 9, 2019 · 1 comment
Labels

Comments

@lmeyer1
Copy link
Contributor

lmeyer1 commented May 9, 2019

Describe the Issue

If a foreign key references two columns, the generated code is not valid. The code generated by ssc creates two alter table statements as follows:

IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE object_id = OBJECT_ID('[dbo].[FK_CR BCG_CR BCD]') AND parent_object_id = OBJECT_ID('[dbo].[CR BCG]'))
BEGIN
    ALTER TABLE [dbo].[CR BCG] WITH NOCHECK ADD CONSTRAINT [FK_CR BCG_CR BCD] FOREIGN KEY ([NRLIGNE]) REFERENCES [dbo].[CR BCD] ([COMPTEUR]) ON DELETE CASCADE ON UPDATE CASCADE
    ALTER TABLE [dbo].[CR BCG] CHECK CONSTRAINT [FK_CR BCG_CR BCD]
END

IF NOT EXISTS (SELECT 1 FROM sys.foreign_keys WHERE object_id = OBJECT_ID('[dbo].[FK_CR BCG_CR BCD]') AND parent_object_id = OBJECT_ID('[dbo].[CR BCG]'))
BEGIN
    ALTER TABLE [dbo].[CR BCG] WITH NOCHECK ADD CONSTRAINT [FK_CR BCG_CR BCD] FOREIGN KEY ([NRFACTURE]) REFERENCES [dbo].[CR BCD] ([NRFACTURE]) ON DELETE CASCADE ON UPDATE CASCADE
    ALTER TABLE [dbo].[CR BCG] CHECK CONSTRAINT [FK_CR BCG_CR BCD]
END

Expected behavior

The code generated should recreate the foreign key. The code to create such a foreign key could look like (code generated by MS SQL Server Management Studio 2008 R2):

ALTER TABLE [dbo].[CR BCG]  WITH NOCHECK ADD  CONSTRAINT [FK_CR BCG_CR BCD] FOREIGN KEY([NRLIGNE], [NRFACTURE])
REFERENCES [dbo].[CR BCD] ([COMPTEUR], [NRFACTURE])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[CR BCG] CHECK CONSTRAINT [FK_CR BCG_CR BCD]
GO
@lmeyer1
Copy link
Contributor Author

lmeyer1 commented Oct 19, 2020

Fixed with PR #135

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants