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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of There is already an object named ‘tblPerson_Gender_FK’ in the database without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

We are here to answer your question about There is already an object named ‘tblPerson_Gender_FK’ in the database - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji