Postgresql: detecting which foreign key triggered the "on before delete" trigger -
let t
table 2 columns: a
, b
, reference, respectively, tables a
, b
. such references of type "delete cascade", if row a
or b
deleted, row in t
matches origin reference deleted.
now, want set trigger "on before delete row" on t
: there way detect of reference triggered row deletion in t
? in other words: can know if trigger triggered cascading a
or b
?
thank you.
edit
ok, i've simplified problem. have following tables:
users:
- id: integer serial primary key
- name: character varying(128)
validatorchains:
- id: integer serial primary key
- name: character varying(128)
validatorchainslinks:
- chain: integer foreign key validatorchains.id on delete cascade
- user: integer foreign key users.id on delete cascade
- next: integer foreign key users.id on delete set null
- prev: integer foreign key users.id on delete set null
the code of "on before delete" trigger on validatorchainslinks
is:
begin update validatorchainslinks set next = old.next next = old.user; update validatorchainslinks set prev = old.prev prev = old.user; return old; end;
so, purpose create linked list of users can validate sort of operation. in order validate operation, users in chain must agree. problem arises when maintaining linked list when user deleted. trigger code above relinks elements of chain. but, happens if deletion triggered deleting row in validatorchains
? don't want trigger update stuff, skip , let system delete rows reference corresponding validator chain.
hope helps.
you can little work-around.
since pg not give relevant information in trigger function fires on cascaded delete
(in validatorchainslinks
) should record information somewhere, prior foreign record (in users
or validatorchains
) being deleted. can write before delete
trigger updates records in validatorchainslinks
value can check in trigger function of latter table. require add field table of course.
alternatively, can create separate table lists records tables deleted , trigger function can review information. write table in before delete
trigger , remove record in after delete
trigger on same table. more elegant solution if same pattern (cascaded deletes) happens among multiple tables.
cascaded deletes happen in single transaction should protected race conditions.
Comments
Post a Comment