Put all foreign key constraints at end of TSQL script

Sep 17, 2012 at 2:53 PM

I see the source shows where this was done, but it is not completely working.

I am running 3.9.4 and there are some constraints added at the end of the script, but there are quite a few spread throughout the script - and causing issues.

Looks to me like the code may be looking for the word 'CHECK' on the constraint, where it actually has NOCHECK and then at the bottom of the script adds the check option in?

Here is an example of what it misses...

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_SomeKey]') AND parent_object_id = OBJECT_ID(N'[dbo].[SomeTable]'))
ALTER TABLE [dbo].[SomeTable]  WITH NOCHECK ADD  CONSTRAINT [FK_SomeKey] FOREIGN KEY([ForeignKey])
REFERENCES [dbo].[SomeForeignTable] ([Pkey])
GO

Then at the bottom of the script...

 

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_SomeKey]') AND parent_object_id = OBJECT_ID(N'[dbo].[SomeTable]'))
ALTER TABLE [dbo].[SomeTable] CHECK CONSTRAINT [FK_SomeKey]
GO

Coordinator
Sep 17, 2012 at 5:54 PM

Hi,

 

Yes, you are correct.  I will have it fixed in the next release.  Hopefully, that will be in the next couple of days.

 

Thanks for your feedback,

George 


Coordinator
Sep 18, 2012 at 3:26 AM

Ok, I think I catch all of the constraints.  Check out the latest version and see if it works.  Let me know if you have any issues.

Thanks,
George