ON DELETE CASCADE Question

From: Jason Long <mailing(dot)lists(at)octgsoftware(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: ON DELETE CASCADE Question
Date: 2013-11-04 19:44:11
Message-ID: 1383594251.14536.6.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I would like for corresponding records in t_a to be deleted when I
delete a record from t_b. This deletes from t_b when I delete from t_a,
but not the other way around. I am unable to create a foreign key
constraint on t_a because this table holds records from several other
tables. I added a simple script below that demonstrates my problem.

Any suggestions?

/*******************************************************************/
drop table IF EXISTS t_b;
drop table IF EXISTS t_a;

CREATE TABLE t_a
(
id bigint NOT NULL,
CONSTRAINT pk_a PRIMARY KEY (id)
);

CREATE TABLE t_b
(
id bigint NOT NULL,
CONSTRAINT pk_b PRIMARY KEY (id),
CONSTRAINT fk_b_a FOREIGN KEY (id) REFERENCES t_a (id) ON DELETE
CASCADE
);

INSERT INTO t_a VALUES (1),(2),(3);
INSERT INTO t_b VALUES (1),(2),(3);

delete from t_b where id = 2;

select * from t_a;

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-11-04 19:48:04 Re: table lock when where clause uses unique constraing instead of primary key.
Previous Message Rob Sargent 2013-11-04 18:51:38 Re: table lock when where clause uses unique constraing instead of primary key.