user962206
user962206

Reputation: 16127

SQL Server: Unable to create relationship

I was trying to create a table that has a one to many relationships. but it seems that adding a foreign key in Personal is not working. I am trying to link a Personal Information table to a address table? what is the solution for this error?

Unable to create relationship 'FK_Personal_Address'.
Cascading foreign key 'FK_Personal_Address' cannot be created where the referencing column 'Personal.ID' is an identity column. Could not create constraint. See previous errors.

Upvotes: 2

Views: 22408

Answers (5)

DevelopZen
DevelopZen

Reputation: 375

I made sure to follow identity, int and primary key discussed in above answer. However, I was still getting the same error.

'xReason' table saved successfully
'xAddress' table
- Unable to create relationship 'FK_xAddress_xReason'.  
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_xAddress_xReason". The conflict occurred in database "databaseName", table "dbo.xReason", column 'xReasonID'.

This error resolved when I inserted some data into a Reason table. (table that had a primary key)

If you read this far, this might be your problem.

Upvotes: 5

user2154869
user2154869

Reputation: 11

I got the same error with adding foreign key constraints to one of my tables. I found the workaround was to add it WITH NOCHECK. why I was able to add the other two foreign keys WITH CHECK but not the third foreign? I found that it was not the table but the order of the foreign key to be added. Any insight to this will be much appreciated.

Upvotes: 0

njr101
njr101

Reputation: 9619

The primary key in the Person table is presumably an identity. This is an auto-incrementing integer field.

You need to make the foreign key in the address table of type int, not identity. It will hold integers which correspond to Person records, but you don't want the foreign key to auto-increment. For each record in the child table (address) you will set a specific value for the foreign key indicating to which parent record (Person) it belongs.

Example:

INSERT person (firstname, lastname) VALUES ('John', 'Smith')

This will insert the new person record and the field personid will be filled automatically because it is an IDENTITYfield.

Now to insert an address from John Smith you need to know his personid. For example:

-- Say, for example, personid of John Smith is 55
INSERT address (personid, street, city) VALUES (55, 'High Street', 'London')

So in the person table the personid is generated automatically but in the address table you specify the value that matches an existing person. That's the whole point of a foreign key.

Without more information about your schema it's hard to guess the problem.

Upvotes: 6

Kilren
Kilren

Reputation: 415

it seem that you try to create a foreign key on Personal.ID related to itself.

You probably want to do something like :

ALTER TABLE Adress  WITH NOCHECK ADD  CONSTRAINT [FK_Adress_Personnal] FOREIGN KEY(Personal_Id)
REFERENCES Personal (ID)

Upvotes: 1

Fls'Zen
Fls'Zen

Reputation: 4654

Without seeing the structure of the tables in the question, I believe the most likely cause is the column in your child table (Address) is marked as an Identity column. In a foreign-key relationship, the parent determines the value of the field, not the child. The column may be the PK in the child table, but not an Identity.

Upvotes: 1

Related Questions