PartitionKey extracted from document doesn't match the one specified in the header on CreateItemAsync
Asked Answered
W

4

18

I have a bit of a problem using Microsoft.Azure.Cosmos version 3.2.0,

upon running

await this.Container.CreateItemAsync<LogEntity>(logEntity, new PartitionKey("anythingIPutHere"));

it throws

Microsoft.Azure.Cosmos.CosmosException HResult=0x80131500
Message=Response status code does not indicate success: 400 Substatus: 1001 Reason: (Message: {"Errors":["PartitionKey extracted from document doesn't match the one specified in the header"]}

but if I put,

await this.Container.CreateItemAsync<LogEntity>(logEntity, new PartitionKey(logEntity.Id));

it works and it is the only case when it works.

I have also tried

  • Putting the value for the partition key as a property in the object;
  • Even specifying a partitionKey JSON property name but no success;

I looked over some guides on the Microsoft site and it seems you can specify the partition key to be some string, not necessary to be the id or specified with a property name on the object; so why is this happening?

Weapon answered 26/9, 2019 at 17:13 Comment(1)
you can specify any field from your JSON document to be partition key not necessarily "id". Partition Key specified while creating the container should be the key value that is passed while creating item. In your case Id is the partition key that was specified while creating container hence it worked when you passed the "id" . Also note - While creating partition key create it in "camelCase" format. e.g. you can specify "pinCode" as partition key – Shinto
W
39

I've overlooked that when I have created the container

this.Container = await this.Database.CreateContainerIfNotExistsAsync("testContainer", "/id");

I have specified partitionKeyPath as being the /id.

It also seems that the partition key must reside on the object and must have a json property of partitionKeyPath property name without / like:

[JsonProperty(PropertyName = "partition")]
public string Partition { get; set; }

if partitionKeyPath is /partition

this.Container = await this.Database.CreateContainerIfNotExistsAsync("testContainer", "/partition");

Sorry if this is obvious, I have just started playing around with CosmoDb.

Weapon answered 26/9, 2019 at 17:54 Comment(7)
Note that, when you create the Container in the Azure Portal, which requires you to specify a PartitionKey as well, you can also directly refer to a property of the documents you want to insert in that container later on. For instance, when your container will store Person documents and the Person class has a property FirstName you can specify the PartitionKey in the portal as /FirstName. In this case, Json annotation [JsonProperty() of the property is not needed. – Epiphora
Thank you, this was helpful! – Nympholepsy
Ahh, I didn't realize it had to be a string; my partition key property was of type PartitionKey (and the two exactly matched, and it was specified correctly and everything)... switching it to a string fixed the issue. πŸ€¦πŸ»β€β™‚οΈ – Raffle
Thanks so much @Raffle - I've been fighting this for a while (before you made this comment) came back to it tonight with fresh eyes, found this, solved in 10 minutes. – Uppity
More about choosing partition key - learn.microsoft.com/en-us/azure/cosmos-db/… – Shinto
@Raffle The partion key value can be "of string or numeric types." But the document is not clear as one would wish on this. My test result is that intigers and string seem working fine while char oddly doesn't. – Barbosa
I had this exact problem. This answer was super helpful and saved me a ton of time. – Common
S
0

Just had a similar issue. In my case the type did not match. In some documents the property I used as a partitionKey had a string value and in others it had an int value.

Stria answered 18/8, 2023 at 18:40 Comment(0)
S
0

Rember that CosmosDb is case sensitive. Meaning that if you inferred serilization and upsert, the property names in you document will be updated to match the capitalization of your class, unless you annotate your properties.

Sapsago answered 20/8, 2023 at 5:28 Comment(0)
N
0

I was seeing this as well when I tried to create a new object using .upsert(myObj). The reason was that I defined my PK as /id and when I created a new object the id property was undefined so cosmos could not find the partition key.

Solution: If you use /id as PK use create and not upsert to create a new item.

Nereidanereids answered 27/5 at 19:36 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.