Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the value of an Id in RavenDB

We have an entity named Organization that we use the UniqueConstraints-bundle on. We have a property named NetName that is a UniqueConstraint and an automaticly generated Id.

Since this is unneccesary we want to use the NetName-property as Id instead. So that we don't need UniqueConstraints to know that it is unique and also get the benefit from being able to use Load when we have the NetName.

We needed to clean up our netname a bit before using it as an Id so we created a new temporary-property called TempUniqueNetName that now holds the value of:

"organizations/"+ CleanupId(this.NetName)

So we are now ready to simply move that value to our Id. But we can't get it to work. Our problem is that with the PatchRequest below we end up with a new property named Id in the database but the acctual Id still has the same value (see screenshot). Is there a better (correct) way to change the value of an Id?

The Entity:

class Organization {
   public string Id { get; set; }

   [UniqueConstraint]
   public string NetName { get; set; }

   public string TempUniqueNetName{ get; set; }
}

We want to do something like this:

_documentStore.DatabaseCommands.UpdateByIndex(typeof(Organizations).Name,
            new IndexQuery(),
            new[]
                    {
                        new PatchRequest()
                            {
                                Type = PatchCommandType.Rename,
                                Name = "TempUniqueNetName",
                                Value = new RavenJValue("Id")
                            }
                    });

Value has not changed

like image 931
Sebastian Hoffback Avatar asked Jan 03 '13 13:01

Sebastian Hoffback


2 Answers

I don't think you can change the document key via patching. It's not actually stored with the document or the metadata - it's copied into the @id metadata on load to give you the illusion that it's there, and the Raven Client copies it again into your own identity property in the document. But really, it's a separate value in the underlying esent document store. Raven would have to know specifically how to handle this and fake it for you.

You could manually copy the doc from the old id to the new one and delete the old, but that could be time consuming.

There isn't a great answer for renaming a document key right now. There really should be a DatabaseCommand for rekeying a single document, and separate PatchCommandType to rekey when patching. Perhaps this will be added to raven in the future.

like image 121
Matt Johnson-Pint Avatar answered Nov 01 '22 18:11

Matt Johnson-Pint


You can check implemtation of PUT-DELETE usage for updating IDs in my github repo. It should look something like this:

store.DatabaseCommands.Put(updatedKey, null, document.DataAsJson, newMetadata);
store.DatabaseCommands.Delete(oldKey, null);

https://github.com/Sevsoad/SagaUpdater/

Also here is some Raven documentation:

https://ravendb.net/docs/article-page/3.0/csharp/client-api/commands/documents/put

like image 29
Siarhei Bialko Avatar answered Nov 01 '22 17:11

Siarhei Bialko