7

I'm trying to add an EXCLUDE constraint to one of my existing tables this is the sql I'm running:

ALTER TABLE appointment_service
  ADD COLUMN start_time bigint,
  ADD COLUMN end_time bigint,
  EXCLUDE USING gist (
            professional_id WITH =,
            int8range(start_time, end_time) WITH &&
  ),
  ADD COLUMN professional_id text REFERENCES professionals ON DELETE CASCADE ON UPDATE CASCADE

And this is the error I get.

ERROR:  syntax error at or near "EXCLUDE"
LINE 4:   EXCLUDE USING gist (

What is the correct SQL syntax to accomplish this?

1
  • @Jonast92: you are wrong. Commented Nov 17, 2017 at 13:52

1 Answer 1

13

You need to add the exclusion constraint separately.

First add the columns:

ALTER TABLE appointment_service
  ADD COLUMN start_time bigint,
  ADD COLUMN end_time bigint,
  ADD COLUMN professional_id text 
     REFERENCES professionals ON DELETE CASCADE ON UPDATE CASCADE;

Then add the constraint:

ALTER TABLE appointment_service
  add constraint unique_professional_id
  EXCLUDE USING gist (
            professional_id WITH =,
            int8range(start_time, end_time) WITH &&
  )
Sign up to request clarification or add additional context in comments.

3 Comments

BTW in single statement it also possible: create table t(x int); alter table t add column y text, add constraint foo exclude (y with =);
@Abelisto: if I try that I get ERROR: column "start_time" does not exist
Hmmm... Seems that it is related to the function usage. EXCLUDE ( professional_id WITH =, start_time with =); works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.