Re: foreign keys with on delete cascade and triggers

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dirk Jagdmann <jagdmann(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: foreign keys with on delete cascade and triggers
Date: 2006-01-12 03:02:21
Message-ID: 331.1137034941@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Dirk Jagdmann <jagdmann(at)gmail(dot)com> writes:
> The idea behind the sample commands below is, that the whole deletion
> should be denied, because a trigger in a cascaded table blocked the
> deletion. The trigger works as expected and prevents rows with a value
> of "5" being deleted from table "b". However if the deletion was
> triggered via the cascaded foreign key (trigger), the deletion in
> table "a" is not rolled back, thus the row with "5" in "a" is lost.
> This of course leaves the database in an inconsistant state, because
> the foreign key in table "b" can no longer be referenced in "a".

> Now I'd like to know if this is a bug in the current form of cascaded
> deletions; or if this is desired behaviour and the oppression of
> deletions via triggers is undefined behaviour in the cascaded case; or
> if this issue just hasn't been addressed yet; or something completly
> differnt.

This is a bug in your trigger design. The database is doing what it's
supposed to do, ie, trying to delete the dependent row. If you turn
that into a no-op, it's your own fault if you don't like the resulting
state.

If you want the whole transaction rolled back, raise an error instead
of returning NULL.

(This has been discussed before ... see the archives.)

regards, tom lane

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rainer Leo 2006-01-12 08:43:59 error code invalid_input_syntax
Previous Message Tony Wasson 2006-01-11 23:51:00 Re: psql client: technique for applying default values to :variables?