I am having a problem creating foreign keys in a couple of new tables. The error message is : - Unable to create relationship 'FK_Rpt_InvoiceHistoryDetail_Rpt_Customer'. The ALTER TABLE statement co

I am having a problem creating foreign keys in a couple of new tables. The error message is :
- Unable to create relationship 'FK_Rpt_InvoiceHistoryDetail_Rpt_Customer'.  
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Rpt_InvoiceHistoryDetail_Rpt_Customer". The conflict occurred in database "MASWork", table "dbo.Rpt_Customer", column 'Id'.
jmalinowskiAsked:
Who is Participating?
 
_agx_Commented:
Say you're creating a foreign key on TABLE_A.CustomerID that links to the primary keyTABLE_B.CustomerID.  To ensure referential integrity, all of the CustomerID values in TABLE_A must exist in TABLE_B. From the error message, it sounds like that's not the case.

EDIT: Example, if the table data were this, you might get that error

TableB  - CustomerID
11
24

Table A - CustomerID
11
14           <=== doesn't exist in parent table, TABLE_B
24
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Are you creating the FK after having data in the tables. If so, then _agx_'s explanation can justify the error.
Otherwise check if the FK column is as the same date type as the PK column from the related table.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
jmalinowski, a feedback will be appreciated.
Cheers
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Solution provided.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.