Re: Foreign Key Constraint Deletion Order

From: <cnliou(at)eurosport(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Foreign Key Constraint Deletion Order
Date: 2001-11-19 07:17:59
Message-ID: 200111190717.3b63@lh00.opsion.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jean-Christophe Boggio 2001-11-19 07:32:54 Re: Optimization with dates
Previous Message Roland Roberts 2001-11-19 04:00:07 Re: PL/pgSQL examples NOT involving functions