Is it possible to conditionally add a foreign key in PostgreSQL?
Something like: ALTER TABLE table1 ADD FOREIGN KEY (some_id) REFERENCES other_table WHERE some_id NOT IN (0,-1) AND some_id IS NOT NULL;
In particular, my reference table has all positive integers (1+), but the table in which I have to add the foreign key can contain zero (0), zero and negative (-1), and everything means something else.
Notes:
I fully understand that this is a poor table design , but it was a tricky trick created 10 years ago, when the opportunities and resources that we had available at the moment did not exist. This system works by hundreds of retail stores, so going back and changing the method at this point can take months that we donβt have.
I canβt use the trigger, it MUST be done using a foreign key .
postgresql
trex005
source share