There is already an object named ‘tblPerson_Gender_FK’ in the database

In SQL Server 2014, I wanted to create a foreign key

ALTER TABLE tblPerson
    ADD CONSTRAINT tblPerson_Gender_FK 
        FOREIGN KEY(Gender) REFERENCES tblGender(ID);

But I always get this error:

Msg 2714, Level 16, State 5, Line 30
There is already an object named ‘tblPerson_Gender_FK’ in the database.

Msg 1750, Level 16, State 0, Line 30
Could not create constraint or index. See previous errors.

This is my table

create table tblPerson
(
    ID int Primary Key NOT NULL,
    Name varchar(max) Not null ,
    Email varchar(max) Not null,
    Gender int
)

and another

create table tblGender
(
     ID int not null Primary key,
     Gender varchar(max) 
)

Answer

The error is pretty clear that you have a another foreign key that named tblPerson_Gender_FK.

You can change the foreign key name;

ALTER TABLE tblPerson
ADD CONSTRAINT tblPerson_Gender_2_FK FOREIGN KEY(Gender) REFERENCES tblGender(ID);

or you can drop existing foreign key. But I don’t recommend it because dropping a existing foreign key might cause unexpected data problems on related tables.

Leave a Reply

Your email address will not be published. Required fields are marked *