Cant insert any data when hierarchial partition key is involved

124 Views Asked by At

I can't seem to insert any data when there is a hierarchial partition key. I keep getting the "(Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document." error but it doesn't make sense as I am providing the right key and document has those values

'key' variable is a string variable containing a guid:

                dynamic document = new 
                {
                    Id = key,
                    ObjectName = "PlayerInfo",
                    PlayerID = key,
                    PlayerInfo = "foobarbaz"
                    // other document properties
                };

                string partitionKeyValue = $"/{document.ObjectName}/{document.PlayerID}";

                ItemResponse<dynamic> resp = await container.CreateItemAsync(document, new PartitionKey(partitionKeyValue));
1

There are 1 best solutions below

2
David Makogon On BEST ANSWER

Since you're using a hierarchical partition key, you need to specify all the parts of the partition key. In your case, maybe construct your partition key with something like this:

var partitionKey = new PartitionKeyBuilder()
   .Add("PlayerInfo")
   .Add("key")
   .Build();
ItemResponse<dynamic> resp = await container.CreateItemAsync(document, partitionKey);

You can compare using PartitionKeyBuilder().Build() output to the partition key value you constructed, to see what the differences are...