pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: zha-vova@yandex.ru
To: pgsql-bugs@postgresql.org
Subject: BUG #14114: FK on-delete-cascade trouble
Date: Tue, 26 Apr 2016 12:44:45 +0000
Message-ID: <20160426124445.2722.57235@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14114
Logged by:          Vladimir Zhavoronkov
Email address:      zha-vova@yandex.ru
PostgreSQL version: 9.4.5
Operating system:   Red Hat 4.8.5-4 64-bit
Description:        

After creating a FK constraint with "on delete cascade" option I got a
problem with before-delete-trigger on a dependent table. This short listing
below will make it clear:

test_db=# create table t (id integer primary key);
CREATE TABLE
test_db=# create table tt (id integer primary key, t_id integer references
t(id) on delete cascade);
CREATE TABLE

--here we have table "t" that is referenced from table "tt" with
on-delete-cascade foreign key

--this before-trigger will prevent deleting rows from table "tt"

test_db=# create or replace
test_db-# function d_tt()
test_db-# returns trigger as $$
test_db$# begin
test_db$#   return null;
test_db$# end;
test_db$# $$ language plpgsql;
CREATE FUNCTION
test_db=# create trigger d_tt before delete on tt for each row execute
procedure d_tt();
CREATE TRIGGER

--insert some data in tables:
test_db=# insert into t(id) values(1);
INSERT 0 1
test_db=# insert into tt(id, t_id) values(1,1);
INSERT 0 1

--so we have a record in table "tt" that references table "t"; then we try
to delete referenced record from table "t":

test_db=# delete from t where id = 1;
DELETE 1

Now we deleted record with id=1 from table "t", but still got records in
table "tt" with t_id=1. This contradicts with our foreign key constraint:


test_db=# select * from t;
 id 
----
(0 rows)


test_db=# select * from tt;
 id | t_id 
----+------
  1 |    1
(1 row)



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)  latest in thread

Message-ID: <20160426124445.2722.57235@wrigleys.postgresql.org>
Permalink:  ../20160426124445.2722.57235@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20160426124445.2722.57235@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: zha-vova@yandex.ru
  Subject: Re: BUG #14114: FK on-delete-cascade trouble
  In-Reply-To: <20160426124445.2722.57235@wrigleys.postgresql.org>
  References: <20160426124445.2722.57235@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox