locked
Unable to save foreign keys RRS feed

  • Question

  • I'm trying to setup my foreign keys so I can use them in a VisualStudio projects but I'm getting an error and I can't track down what's causing it.  Can anyone help me with this?

    'tbl_departments' table saved successfully
    'tbl_people' table saved successfully
    'tbl_locations' table saved successfully
    'tbl_position' table
    - Unable to create relationship 'FK_tbl_position_tbl_departments'.  
    The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_tbl_position_tbl_departments". The conflict occurred in database "MyProjectDB", table "dbo.tbl_departments", column 'ID'.

    All of my tables have an ID field that is a primary key with Allow Null unchecked.


    Friday, September 4, 2015 2:46 PM

Answers

  • Hi,

    SQL Server always checks data before creating foreign keys. You have departmnets ids in position table which doesnt exist in department table most likely.

    • Proposed as answer by Naomi NEditor Friday, September 4, 2015 3:12 PM
    • Marked as answer by That Web Guy Friday, September 4, 2015 6:55 PM
    Friday, September 4, 2015 2:57 PM

All replies

  • I forgot to add that if I take out one relationship at a time, the error message says the same thing for each one.
    Friday, September 4, 2015 2:48 PM
  • Hi,

    SQL Server always checks data before creating foreign keys. You have departmnets ids in position table which doesnt exist in department table most likely.

    • Proposed as answer by Naomi NEditor Friday, September 4, 2015 3:12 PM
    • Marked as answer by That Web Guy Friday, September 4, 2015 6:55 PM
    Friday, September 4, 2015 2:57 PM
  • Hi Naomi,

    You are correct.  A friend gave me some code to run to find my abandoned entries since no foreign keys were previously setup.  Thank you for the help.

    Friday, September 4, 2015 6:55 PM