PostgreSQL: How to revalidate CHECKs -
my database has following structure:
create type instrument_type enum ( 'stock', ... 'currency', ... ); create function get_instrument_type(instrument_id bigint) returns instrument_type language plpgsql stable returns null on null input $$ begin return (select instr_type instruments id = instrument_id); end $$; create table instruments ( id bigserial primary key, instr_type instrument_type not null, ... ); create table countries_currencies ( ... curr bigint not null references instruments (id) on update cascade on delete cascade check (get_instrument_type(curr) = 'currency'), ... );
as can see, use 1 common table instruments. there lot of foreign keys referencing table. tables countries_currencies require referenced item 'currency'. since can't use subqueries in check constraints, have use function. 1 day happen 1 bad man change instrument_type 'currency' else. if there row in table countries_currencies, referencing modified instrument, check become invalid row. check applied new rows, not existing.
is there standard way revalidate checks? want run such procedure part of general data integrity test.
p.s. know, write trigger on table instruments , forbid change if become broken. requires assurance check referencing tables , constraints, error prone anyway.
you update rows in place trigger check:
update countries_currencies set curr = curr;
Comments
Post a Comment