In SQL Server , I got this error ->
SQL71516 :: The referenced table '[dbo].[PostsTags]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column
is a computed column, it should be persisted.
I don't understand why dint works foreign key, without them table created without problems, but I need a relationship between tables, in this location.I looked at other similar questions, but the answers were not found.
CREATE TABLE [dbo].[PostsTags] (
[PostId] INT NOT NULL,
[TegId] INT NOT NULL,
CONSTRAINT [PK_PostsTags] PRIMARY KEY CLUSTERED ([PostId] ASC, [TegId] ASC)
);
CREATE TABLE [dbo].[Comments] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[PostId] INT NOT NULL,
[DateTime] DATETIME NOT NULL,
[Name] NVARCHAR (64) NOT NULL,
[Body] NVARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Tags] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (64) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Tags_PostsTags] FOREIGN KEY ([Id]) REFERENCES [PostsTags]([TegId]) ON DELETE CASCADE
);
CREATE TABLE [dbo].[Posts] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (128) NOT NULL,
[DateTime] DATETIME NOT NULL,
[Body] NVARCHAR (MAX) NOT NULL,
[Avtor] NVARCHAR (64) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [Post_Tag] FOREIGN KEY ([Id]) REFERENCES [PostsTags]([PostId]) ON DELETE CASCADE,
CONSTRAINT [Post_Comment] FOREIGN KEY ([Id]) REFERENCES [Comments]([PostId]) ON DELETE CASCADE,
);
Sorry for bad English.