I'm wondering about the execution/firing order of triggers for FKs with UPDATE CASCADE, concerning grandchild tables, in PostgreSQL (9.3)
Here's what I have:
Parent: Invoices (invoice_id)
Child: Invoice_Lines (invoice_id, line_nbr)
Grandchild: Invoice_Line_Taxes (invoice_id, line_nbr, tax)
Invoice_Lines has FOREIGN KEY (invoice_id) REFERENCES Invoices ON UPDATE CASCADE;
Invoice_Line_Taxes has FOREIGN KEY (invoice_id, line_nbr) REFERENCES Invoice_Lines ON UPDATE CASCADE;
I also have a custom UPDATE trigger on Invoices (who's name start with S, which comes after RI_). This trigger sums up the invoice amounts. And my actual update statement changes invoice_id, which gets propagated down to the child and grandchild.
Trouble is, in/during my custom trigger, the invoice_id for Invoice_Lines has already changed, but not for the grandchild Invoice_Line_Taxes.
I've dumped the rows from within the custom trigger using RAISE:
invoice_lines: (5,1)
invoice_line_taxes: (-1,1,HST)
After the trigger:
SELECT * from invoice_line_taxes where invoice_id IN (5,-1);
invoice_id | line_nbr | tax_nm
------------+----------+--------
5 | 1 | HST
So I'm wondering, what's the trigger execution order with respect to cascading triggers?
I would have assumed something like this:
RI_on_invoices
RI_on_invoice_lines
S_custom_trigger
Any ideas? Anybody know where I can get official documentation on the execution ordering? I've tried looking for detailed documentation about this, but all I've found is that ordering is alphabetical. Perhaps if I find the specific details about the ordering I could build something around it. But right now, I'd be basing it on guess work.
Thank you.