Thursday, March 10, 2016

There are no primary or candidate keys in the referenced table 'dbo.Foo' that match the referencing column list in the foreign key 'FK_Bar'.

Good news! This is an easy T-SQL bug to beat. Before you can run something like this...

ALTER TABLE dbo.Foo ADD CONSTRAINT
   FK_Bar FOREIGN KEY
   (
   BazId
   ) REFERENCES dbo.Baz (
   BazId
   ) ON UPDATE NO ACTION ON DELETE NO ACTION

 
 

...you must remember to run something like this:

ALTER TABLE dbo.Baz ADD CONSTRAINT
   PK_Baz PRIMARY KEY CLUSTERED
   (
   BazId
   ) WITH (
   STATISTICS_NORECOMPUTE = OFF,
   IGNORE_DUP_KEY = OFF,
   ALLOW_ROW_LOCKS = ON,
   ALLOW_PAGE_LOCKS = ON
   ) ON [PRIMARY]

No comments:

Post a Comment