Several unique constrains on a single document in RavenDb

430 views Asked by At

Let's say I have a class:

public class Person
{
   public string Name{get;set;}
   public string Email {get;set;}
   public string GoogleId {get;set;}
   public string FacebookId {get;set;}
}

If I want to make the email unique I will use the unique constraint bundle.

But I want to make both the googleId and the facebookId properties as a single unique constraint side by side with the email constraint (while non of them is the id). Is it possible?

2

There are 2 answers

0
user3255018 On BEST ANSWER

Use the UniqueConstraints bundle:

public class Person
{
   public string Name {get;set;}
   [UniqueConstraint]
   public string Email {get;set;}
   public string GoogleId {get;set;}
   public string FacebookId {get;set;}
   [UniqueConstraint]
   public string GoogleAndFacebookIds { get;set; }
}

Just make sure you update GoogleAndFacebookIds everytime you update either GoogleId or FacebookId. I was doing this so much I ended up using a simple interface on all my classes that did this sort of thing:

public interface ICombinedConstraints
{
    string UniqueId { get; set; }
    void UpdateConstraints();
}

So,

public class Person : ICombinedConstraints
{
    public string Name{get;set;}
    [UniqueConstraint]
    public string Email {get;set;}
    public string GoogleId {get;set;}
    public string FacebookId {get;set;}
    [UniqueConstraint]
    public string UniqueId { get; set; }

    public void UpdateConstraints()
    {
        UniqueId = GoogleId + FacebookId;
    }
}
0
Jevgenij Nekrasov On

You cannot do that. Raven doesn't provide any possibility to enforce unique constraints on the property. If you want to do that you need separate sets of documents (see link below)

Unique constraints in Raven

Update:

It seems that you can also try to use bundles to implement unique constraints in the object.

Bundle: Unique constraints