Those are the two tables:
create table sales.SpecialOfferProduct
(
SpecialOfferID int not null,
ProductID int not null,
rowguid uniqueidentifier not null,
ModifiedDate datetime not null,
primary key (specialofferid, productid)
)
create table sales.SalesOrderDetail
(
SalesOrderID int not null,
SalesOrderDetailId int not null,
CarrierTrackingNumber nvarchar(25),
OrderQty smallint not null,
ProductId int not null,
SpecialOfferId int not null,
UnitPrice money not null,
UnitPriceDiscount money not null,
LineTotal as (isnull(([UnitPrice]*((1.0)-[UnitPriceDiscount]))*[OrderQty], (0.0))),
rowguid uniqueidentifier not null,
ModifiedDate datetime not null,
primary key (SalesOrderID, SalesOrderDetailId)
)
I'm trying to add a foreign key :
alter table sales.SalesOrderDetail
add foreign key (ProductId)
references sales.SpecialOfferProduct(ProductId)
I get this error :
Msg 1776, Level 16, State 0, Line 180
There are no primary or candidate keys in the referenced table 'sales.SpecialOfferProduct' that match the referencing column list in the foreign key 'FK__SalesOrde__Produ__4E88ABD4'.Msg 1750, Level 16, State 1, Line 180
Could not create constraint or index. See previous errors.