Subject | Creation order of foreign keys |
---|---|
Author | Salvatore Besso |
Post date | 2003-09-02T17:24:33Z |
hello all,
I have created a database that is working fine. This is a little
extract (meta code, not real SQL code) of the FK it contains:
Table 3 FK, references table 1 on delete cascade
Table 4 FK, references table 1 on delete cascade
Table 5 FK, references table 1 on delete cascade
Table 2 FK, references table 1 on delete cascade
It happened that I moved the last FK at the first place into the
script and I created the database again from scratch:
Table 2 FK, references table 1 on delete cascade
Table 3 FK, references table 1 on delete cascade
Table 4 FK, references table 1 on delete cascade
Table 5 FK, references table 1 on delete cascade
When I delete a row in table 1 I get a constraint violation on a field
in table 5 that can't be null. At a first look, the error is generated
in the after delete trigger for table 2 when the totals in table 5 are
going to be automatically updated after some calculations.
If I return back to the original order of FK's, the row deletion in
table 1 and the consequent cascades are executed without errors.
So I was wondering if creation order is important, especially when
after delete triggers are used in tables where FK's are also used. I
have found nothing in the documentation (Interbase 6 manuals) about
this argument.
I have not posted the entire SQL script because it is very long, but
if necessary I can do it.
Thanks
Salvatore
I have created a database that is working fine. This is a little
extract (meta code, not real SQL code) of the FK it contains:
Table 3 FK, references table 1 on delete cascade
Table 4 FK, references table 1 on delete cascade
Table 5 FK, references table 1 on delete cascade
Table 2 FK, references table 1 on delete cascade
It happened that I moved the last FK at the first place into the
script and I created the database again from scratch:
Table 2 FK, references table 1 on delete cascade
Table 3 FK, references table 1 on delete cascade
Table 4 FK, references table 1 on delete cascade
Table 5 FK, references table 1 on delete cascade
When I delete a row in table 1 I get a constraint violation on a field
in table 5 that can't be null. At a first look, the error is generated
in the after delete trigger for table 2 when the totals in table 5 are
going to be automatically updated after some calculations.
If I return back to the original order of FK's, the row deletion in
table 1 and the consequent cascades are executed without errors.
So I was wondering if creation order is important, especially when
after delete triggers are used in tables where FK's are also used. I
have found nothing in the documentation (Interbase 6 manuals) about
this argument.
I have not posted the entire SQL script because it is very long, but
if necessary I can do it.
Thanks
Salvatore