user2395365
user2395365

Reputation: 2149

Postgres DB constraint for a value of a column

I have a UNIQUE constraint for columns username + user_status, such that we never allow two users that have the same name and status. However I wish this to apply only to the case where the status field is 'ACTIVE' but allow any number of records with the same username and status of 'INACTIVE'. Is this possible?

Upvotes: 0

Views: 43

Answers (1)

user330315
user330315

Reputation:

You can create a filtered unique index:

create unique index on the_table (username)
where user_status = 'ACTIVE';

Upvotes: 5

Related Questions