I have implemented this code to add a check constraint after creating the table
ALTER TABLE recruitment_data.candidate_experiences
ADD CONSTRAINT chk_experience_dates
CHECK ( experience_end_date IS NULL
OR experience_end_date >= experience_start_date);
but it gives an error when I run it more than once:
ERROR: constraint "chk_experience_dates" for relation "candidate_experiences" already exists
Since it is impossible to run ADD CONSTRAINT IF NOT EXISTS, is there any other way to run it twice without creating duplicate constraints, and without having to catch and handle or ignore the error?
What should I do to make this code re-runnable?
insertstatements, demonstrating which are allowed and which this constraint is supposed to prevent. What you're showing is perfectly valid but your mention ofif not existssuggests you want this constraint to be able to compare against other rows, which acheckisn't supposed to do. My guess is you want some sort ofexcludeorunique..without overlaps.ERROR: constraint "chk_experience_dates" for relation "candidate_experiences" already exists. It is adding the constraint when this code is executed for the first time, but if I try to execute it one more time, it gives error that I have written above. Since it is impossible to writeADD CONSTRAINT IF NOT EXISTS, is there any other way to run it twice without producing duplicate or error that it already exists?