I have two tables p
and u
as following: (PostgreSQL 9.3)
CREATE TABLE p
(
pid integer NOT NULL,
uid integer,
CONSTRAINT p_fkey FOREIGN KEY (uid)
REFERENCES u (uid) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
);
CREATE TABLE u
(
uid integer NOT NULL,
pid integer,
CONSTRAINT u_fkey FOREIGN KEY (pid)
REFERENCES p (pid) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
);
In p
I have:
pid uid
161556 176266
in u
I have
uid pid
176266 161556
I want to do:
DELETE FROM u WHERE uid=176266;
DELETE FROM p WHERE pid=113116;
But I cant.
ERROR: update or delete on table "u" violates foreign key constraint "p_fkey" on table "p" DETAIL: Key (uid)=(176266) is still referenced from table "p".
I understand the error but I don't know what I can do to make the delete.
Suggestions?