I'm currently in a battle with Entity Framework over trying to make a self-referencing relationship without having to add additional columns/properties. I have confirmed the foreign key I envision is possible in SQL Server, but it is difficult to research possible solutions because I keep finding generic single-column foreign keys.
Is there some widely used term specific to this form of self-referencing, composite foreign key?
create table LookupValues( LookupName nvarchar(255) not null, ExternalRefCode nvarchar(255) default N'' not null, ParentExternalRefCode nvarchar(255), constraint PK_LookupValues primary key (LookupName, ExternalRefCode))alter table LookupValues add constraint CompositeFK foreign key (LookupName, ParentExternalRefCode) references LookupValues
The idea behind it is that although there are multiple values for LookupName
in this table, there will never be cross-value relationships in the parent/child relationships. e.g. a child from LookupName: A
must belong to a parent from LookupName: A
.
I've tried searches like "self-referencing composite foreign key", "self-referencing foreign key using part of primary key", and "composite foreign key with common column in primary key", etc.