From: PegoraroF10 <marcos@f10.com.br>
To: pgsql-general@postgresql.org
Subject: Problem with trigger makes Detail record be invalid
Date: Thu, 19 Apr 2018 10:55:34 -0700 (MST)
Message-ID: <1524160534707-0.post@n3.nabble.com> (raw)
I´m using Postgres 10 on ubuntu.
suppose a simple Master/Detail structure like this:
create table Master(ID integer primary key, name text);
create table Detail(ID integer primary key, Master_ID Integer, OtherInfo
text);
alter table Detail add constraint FKMasterDetail foreign key (Master_ID)
references Master(ID) on update cascade on delete cascade;
Then insert some records on it:
insert into Master(ID, Name) values(1,'First'), values(2,'Second');
insert into Detail(ID, Master_ID, OtherInfo) values(1,1,'Detail
Information'), (2,2,'Detail Information2');
Then, if I delete on Master will delete on detail too. Fine.
delete from Master where ID=1;
But now, suppose I have a complex trigger before update or delete that runs
on Detail table.
create function DoAComplexJobOnDetail() returns trigger as $$
begin
-- Do lots of things then
Return new; --This is the error, because I´m returning new even for
delete;
end;$$ language plpgsql;
create trigger DetailDoAComplexJob before update or delete on Detail for
each row execute procedure DoAComplexJobOnDetail();
Then try to delete the other Master record. It will be deleted on Master but
Detail record doesn´t and will obviously become invalid because the foreign
key.
delete from Master where ID=2;
select * from Master; --will show no records.
select * from Detail; --will show one record pointing to Master_ID=2, that
doesn´t exist anymore.
Is this a bug or it´s mine responsability to check that trigger result ?
If that trigger responds incorrectly I think that no information could be
executed.
--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
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-general@postgresql.org
Cc: marcos@f10.com.br
Subject: Re: Problem with trigger makes Detail record be invalid
In-Reply-To: <1524160534707-0.post@n3.nabble.com>
References: <1524160534707-0.post@n3.nabble.com>
* 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