Subject | RE: [ib-support] Trigger Order (correct question) |
---|---|
Author | Brian K. Woods |
Post date | 2003-02-05T15:30:40Z |
> Sorry, i didn't knew how to express myself.Valdir,
>
> What i'm trying to ask is why a sql statement like 'select master.status
> from master where master.id = detail.id_master' return NULL when inside an
> after delete trigger of a table that is being deleted by a cascade delete
> FK?
The answer: because the deletion of the detail record is happening AFTER
the deletion of the Master.
If you look under the covers, creating a foreign key reference with delete
action CASCADE also
creates an AFTER DELETE trigger position 1. (you can see this using
IBExpert or similar db managers, but you
may have to look at the individual field's dependencies to see the trigger
listed, at least for IBExpert)
As you should relealize, AFTER means the master record is long gone.
For your query in the AFTER delete trigger of the detail record to work,
you would have to change the details record's foreign key delete action to
NO ACTION and
handle the deletion of the detail records yourself in a BEFORE delete
trigger on the
Master record.
So, for your situation, the actual order of events is:
Master Before delete triggers fire
**master record deleted here**
master after delete triggers fire,
one of which is a hidden CHECKxx trigger that deletes all detail records
for the master record
each of these detail deletions causes:
Detail before delete triggers fire
**detail record deleted**
detail after delete triggers fire
-wherein your after delete trigger tries to run a query on
the master that was deleted way back in the
event sequence
HTH,
Brian