ravendbravendb-studio

RavenDb UniqueConstraint doesn't work


I'm using RavenDb Server and Client 3.5.0 and I have tried to get UniqueConstraint work without success.

The simple case:

using Raven.Client.UniqueConstraints;
public class User {
    public string Id { get; set; }
    [UniqueConstraint]
    public string Email { get; set; }
}

The documentation says:

Drop the Raven.Bundles.UniqueContraints assembly in the Plugins directory.

I did it by NuGet: Install-Package RavenDB.Bundles.UniqueConstraints -Version 3.5.0 and then paste the binary Raven.Bundles.UniqueConstraints.dll to folder Plugins that I created myself in Raven's root directory.

After save an User document I get this in Metadata:

"Ensure-Unique-Constraints": [
        {
            "Name": "Email",
            "CaseInsensitive": false
        }
    ]

All seems to work, but I still saving documents with the same email.

UniqueConstraintCheckResult<User> checkResult = session.CheckForUniqueConstraints(user);

                    // returns whether its constraints are available
                    if (checkResult.ConstraintsAreFree())
                    {
                        session.Store(user);
                        session.SaveChanges();
                    }

I check this link RavenDB UniqueConstraint doesn't seem to work and this one https://groups.google.com/forum/#!searchin/ravendb/unique|sort:relevance/ravendb/KzO-eIf9vV0/NJyJ4DNniFUJ and many other that people have the same problem without solution. In some cases they said that are checking if the property already exist in database manualy as solution.

The documentation also says:

To activate unique constraints server-wide, simply add Unique Constraints to Raven/ActiveBundles configuration in the global configuration file, or setup a new database with the unique constraints bundle turned on using API or the Studio

but with no clue how to do that. I did some search and find a possible how: In Studio, select database, go to Settings -> Database settings, and I found this config:

{
    "Id": "TestRaven",
    "Settings": {
        "Raven/DataDir": "~\\TestRaven"
    },
    "SecuredSettings": {},
    "Disabled": false
}

and I tried add this config:

"Settings": {
        "Raven/DataDir": "~\\TestRaven",
        "Raven/ActiveBundles": "UniqueConstraints"
}

Then I get an error when trying save it. The message erros says something like "the database is already created and cant modify or add bundles" and make a sugestion to add this line "Raven-Temp-Allow-Bundles-Change": true and I was able to save de settings with UniqueConstraint bundle configuration.

So far I think I did all requirement that documentation describe. The last one is:

Any bundle which is not added to ActiveBundles list, will not be active, even if the relevant assembly is in the Plugins directory.

The only place that I found a bundle list is creating a new database in Studio, but the list is not editable, just an information about what already has enabled.

The documentation says a lot of requirements but just dont tell us how to do it, super smart, and we have to try gess how. I could get to here so far, but gess what? It still not working!

My question is, UniqueConstraints realy work in RavenDb? Have someone get this working?

If yes, cloud please tell me how?

Thank you in advance!

[Edited] I forgot to mention that I added the follow line:

store.Listeners.RegisterListener(new UniqueConstraintsStoreListener());

And also tried with version 3.5.1.


Solution

  • The issue is that the specified name of the bundle is incorrect so it won't be active on the server side. Please use "Unique Constraints" instead of "UniqueConstraints" in "Raven/ActiveBundles" settings option.