0

I have an Application type in my app.

public class Application {
   private String id;
   private String creator;
   private boolean active;
}

active field marks is current application actual for certain user. Every user is able to have only one application in active(active == true) state and any number of inactive applications(active == false).

Is it possible to deny storing second application record with same creator value and active == true, but let save any number of applications with same creator value and active == false on DB level(Postgres)?

Thanks.

1 Answer 1

2

Yes, a partial unique index can solve this:

CREATE UNIQUE INDEX active_application_per_user ON application (creator) WHERE active;
Sign up to request clarification or add additional context in comments.

Comments

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.