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.
asked Jun 4, 2020 at 15:22
1 Answer 1
You can use a partial unique index:
create unique index on rollouts (device_id)
where is_active;
answered Jun 4, 2020 at 15:46
user1822user1822
-
Thanks, will try that out.Michael Böckling– Michael Böckling2020年06月04日 19:03:39 +00:00Commented Jun 4, 2020 at 19:03
lang-sql