Wolwgang
Wolwgang

Reputation: 111

Azure Function - "PartitionKey extracted from document doesn't match the one specified in the header" on container created in portal

I'm trying to upload some data into Azure Cosmos Db container via CreateItemAsync() method, with this code:

    Database partnersDb = GetCosmosDb();
    Container partnersContainer = GetCosmosContainer(partnersDb);

    try
    {
        ItemResponse<PartnerInfo> partnersResponse = await partnersContainer.CreateItemAsync(partner, new PartitionKey(partner.Id));
    }
    catch (CosmosException e) when (e.StatusCode == HttpStatusCode.Conflict)
    {
        logger.LogInformation($"Item with id {partner.Id} already exists in partners database");
    }

The problem is, that upon trying to upload, i get "PartitionKey extracted from document doesn't match the one specified in the header" error mentioned in the title. I've read similar topics about this, and wasn't able to find out what's wrong. I'm trying to pass value as partitionKey that is defined as [JsonProperty(PropertyName = "id")], moreover, container I'm trying to upload into has been created by someone in azure portal, and I do not know what PartitionKey is specified. Upon trying to run

select c.partitionkey from c

In cosmos db, i get only this for 3 items that have been created manually via "New item" option:

[
    {},
    {},
    {}
]

Any ideas?

Upvotes: 1

Views: 3661

Answers (1)

Steve Johnson
Steve Johnson

Reputation: 8660

This error causes by two value of PartitionKey(within your document and CreateItemAsync method) are not same.

You can get PartitionKeyPath by using this code

ContainerProperties properties = await container.ReadContainerAsync();             
Console.WriteLine(properties.PartitionKeyPath);

Then pass the value of the same name that was after slash '/' as a partitionKey to CreateItemAsync method can solve this error.

Upvotes: 3

Related Questions