Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this a cyclical foreign key constraint?

Tags:

sql

sql-server

I came upon this code, marked "error," in an application I'm to update. Running it on a test database gives a cyclical reference error:

The referential relationship will result in a cyclical reference that is not allowed (Constraint name = descriptions_fk_2)

I named the constraints to see which one caused the problem.

CREATE TABLE items (
id INT NOT NULL UNIQUE IDENTITY,
name NCHAR(100) NOT NULL UNIQUE,
PRIMARY KEY (id)
);

CREATE TABLE sources (
id INT NOT NULL UNIQUE IDENTITY,
item_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (item_id)
REFERENCES items(id) ON UPDATE NO ACTION ON DELETE CASCADE
);

CREATE TABLE descriptions (
id INT NOT NULL UNIQUE IDENTITY,
item_id INT NOT NULL, 
source_id INT NOT NULL,
PRIMARY KEY (id),
CONSTRAINT descriptions_fk_1 FOREIGN KEY (item_id)
REFERENCES items(id) ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT descriptions_fk_2 FOREIGN KEY (source_id)
REFERENCES sources(id) ON UPDATE NO ACTION ON DELETE CASCADE
);

Why is this a cyclical reference? The descriptions table is linked to two separate tables, but none of them link back to descriptions.

like image 847
Ricardo Altamirano Avatar asked Jul 09 '12 15:07

Ricardo Altamirano


People also ask

What is the constraint of foreign key?

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.

What is circular reference in SQL?

In the world of relational databases circular references are schema structures where foreign keys relating the tables create a loop. Circular references cause special types of issues when trying to synchronize two relational database where the foreign keys are enforced.

What is a foreign key constraint Why are such constraints important what is referential integrity?

A foreign key is a column (or combination of columns) in a table whose values must match values of a column in some other table. FOREIGN KEY constraints enforce referential integrity, which essentially says that if column value A refers to column value B, then column value B must exist.


1 Answers

It's not strictly cyclical - but there are multiple cascade paths. So you could cascade delete a row in items two ways:

1) description -> item
2) description -> source -> item

And, for that reason, it's disallowed.

I believe it's a performance concern, as PostGres will allow cycles like that and will just work it out, but deletes under those circumstances can be quite slow.

For some further reading about why it's disallowed, please see this answer.

like image 99
Matt Whitfield Avatar answered Oct 14 '22 11:10

Matt Whitfield