문제

Given this table:

CREATE TABLE rollouts
(
    id           UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
    device_id    TEXT        NOT NULL,
    is_active    BOOLEAN     NOT NULL DEFAULT FALSE
);

How can I ensure that for a given device_id, there is only ever one active rollout? Multiple inactive ones are fine, so a simple unique index doesn't cut it.

도움이 되었습니까?

해결책

You can use a partial unique index:

create unique index on rollouts (device_id) 
where is_active;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top