I am having multiple scheme defined, and while table creation I need to add conditional constraint that the constraint should only be added if the table and scheme exists in PostgresSQL. Below is the bit snippet which I am trying,
CREATE TABLE second.second_table
(
id serial NOT NULL,
another_id integer NOT NULL,
some_column boolean NOT NULL,
CONSTRAINT other_pkey PRIMARY KEY (id),
CONSTRAINT fk_other_another_id FOREIGN KEY (another_id) REFERENCES first.first_table (id)
MATCH SIMPLE ## CHECK (TABLE IF EXISTS first.first_table) ##
)
The conditional part I defined as '##', and its just example and not correct way of writing. So, basically the constraint should be only added if the scheme and table is exists else not. Thanks in advance. Just a note here, I am using Docker of PostgresSQL of 'postgres:11.5-alpine' if something related to version.
The overview about this conditional constraint is in our project we having single PostgresSQL with multiple scheme defined there. As defined above, there is some relation with one scheme to another scheme. Now, while in production environment definitely all schemes will be there with no issue. But while doing some Junit test cases, if you doing locally one scheme can be exists or not. Just to have the dependency constraint I would like to make the constraint as conditional. If the scheme is already created then constraint will be exists or not. As locally if first scheme project have not executed then the constraint will not be added, if first scheme is exists then constraint will also exists.