pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: cnliou@eurosport.com
To: pgsql-sql@postgresql.org
Subject: Re: Foreign Key Constraint Deletion Order
Date: Mon, 19 Nov 2001 07:17:59 GMT
Message-ID: <200111190717.3b63@lh00.opsion.fr> (raw)

Thanks! Stephan,

> We've been having discussions on hackers about the
behavior,
> but it's unlikely that the rows will be deleted
before
> the master row.

Then I am in big big trouble! Please imagine a very
usual scenario:

create table PurchaseMaster (book text primary key,
UnitPrice float);

create table PurchaseDetail (
CONSTRAINT fk_abc FOREIGN KEY (book) REFERENCES
PurchaseMaster (book) on delete cascade on update
cascade,
primary key (book,buyer),
book text, 
buyer text,
quantity smallint
);

create table HowMuchIOwe (buyer text primary
key,amount float);

CREATE FUNCTION MyTrigger() RETURNS opaque AS '
BEGIN
  UPDATE HowMuchIOwe set
amount=amount-old.quantity*(select UnitPrice from
PurchaseMaster where book=old.book)
  where buyer=old.buyer;
END;

CREATE TRIGGER TriggerDetail AFTER DELETE ON
PurchaseDetail FOR EACH ROW EXECUTE PROCEDURE
MyTrigger(
);

Now when a row in PurchaseMaster is deleted by user,
because:

(1) getting rid of fk_abc constraint and replacing it
with a custom trigger function associated with
PurchaseMaster in order to delete PurchaseDetail
"manually" does not work either since PurchaseMaster
row may be deleted BEFORE this custom trigger
function is called;

(2) and row in PurchaseMaster may be deleted before
PurchaseDetail.

so I have no way out!

CN 

--------------------------------------------------------
You too can have your own email address from Eurosport.
http://www.eurosport.com








view thread (8+ messages)  latest in thread

Message-ID: <200111190717.3b63@lh00.opsion.fr>
Permalink:  ../200111190717.3b63@lh00.opsion.fr/
Also on:    postgresql.org/message-id/200111190717.3b63@lh00.opsion.fr

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: cnliou@eurosport.com
  Subject: Re: Foreign Key Constraint Deletion Order
  In-Reply-To: <200111190717.3b63@lh00.opsion.fr>

* 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