Answer the question
In order to leave comments, you need to log in
How to create a unique index with a condition?
id | contractor_id | is_main (bool)
-------------------------------------------------------
1 | 1 | true
2 | 2 | true
3 | 1 | true
Answer the question
In order to leave comments, you need to log in
That's literally how it's done
create unique index on tablename (contractor_id) where is_main;
You can make an index on two columns at once:
CREATE TABLE example (
id SERIAL PRIMARY KEY,
contractor_id INT NOT NULL,
is_main BOOLEAN NOT NULL,
UNIQUE (contractor_id, is_main)
);
CREATE UNIQUE INDEX CONCURRENTLY contractor_id_main ON table_name (contractor_id, is_main);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question