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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.