Conditional PostgreSQL foreign key -
is possible in postgresql conditionally add foreign key?
something like:alter table table1 add foreign key (some_id) references other_table some_id not in (0,-1) , some_id not null;
specifically, reference table has positive integers (1+) table need add foreign key can contain 0 (0), null , negative 1 (-1) instead, meaning different.
notes:
i aware poor table design, clever trick built 10+ years ago when features , resources have available @ point did not exist. system running hundreds of retail stores going , changing method @ point take months don't have.
i can not use trigger, this must done foreign key.
you can add "shadow" column table1 holds cleaned values (i.e. 0 , -1). use column referential integrity checks. shadow column updated/filled simple trigger on table1 writes values 0 , -1 shadow column. both 0 , -1 mapped null.
then have reference integrity , unchanged original column. downside: have little trigger , redundant data. alas, fate of legacy schema!
Comments
Post a Comment