Search code examples
azuregoazure-cosmosdb

Error with mismatched CosmosDB partition key values between the document and header even though the values match


I am using the Azure-SDK-for-Go package azcosmos to create an item in a CosmosDB container. This is the error I currently get:

--------------------------------------------------------------------------------
RESPONSE 400: 400 Bad Request
ERROR CODE: BadRequest
--------------------------------------------------------------------------------
{
  "code": "BadRequest",
  "message": "Message: {\"Errors\":[\"PartitionKey extracted from document doesn't match the one specified in the header. Learn more: https:\\/\\/aka.ms\\/CosmosDB\\/sql\\/errors\\/wrong-pk-value\"]}\r\nActivityId: 9ef3ec05-b381-48c8-bd4e-96a7cb764041, Request URI: /apps/d27ef9bf-18ce-4431-b8de-709648aab568/services/2c472c3b-bd86-4593-8539-814c29caac51/partitions/31299a87-b895-4b13-91c0-788756ca5ff3/replicas/132790818155726834p/, RequestStats: \r\nRequestStartTime: 2023-02-23T20:53:15.4424439Z, RequestEndTime: 2023-02-23T20:53:15.4424439Z,  Number of regions attempted:1\r\n{\"systemHistory\":[{\"dateUtc\":\"2023-02-23T20:52:06.4715437Z\",\"cpu\":1.012,\"memory\":479419988.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0224,\"availableThreads\":32764,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":431},{\"dateUtc\":\"2023-02-23T20:52:16.4816322Z\",\"cpu\":2.342,\"memory\":480026956.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0193,\"availableThreads\":32761,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":431},{\"dateUtc\":\"2023-02-23T20:52:26.4918299Z\",\"cpu\":1.534,\"memory\":480000572.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0158,\"availableThreads\":32764,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":437},{\"dateUtc\":\"2023-02-23T20:52:36.5019603Z\",\"cpu\":1.490,\"memory\":480000576.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0161,\"availableThreads\":32737,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":438},{\"dateUtc\":\"2023-02-23T20:52:46.5121122Z\",\"cpu\":1.306,\"memory\":479989504.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0204,\"availableThreads\":32762,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":438},{\"dateUtc\":\"2023-02-23T20:53:06.5323276Z\",\"cpu\":1.561,\"memory\":479914676.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0113,\"availableThreads\":32763,\"minThreads\":52,\"maxThreads\":32767},\"numberOfOpenTcpConnection\":424}]}\r\nRequestStart: 2023-02-23T20:53:15.4424439Z; ResponseTime: 2023-02-23T20:53:15.4424439Z; StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westus1-fd44.documents.azure.com:14323/apps/d27ef9bf-18ce-4431-b8de-709648aab568/services/2c472c3b-bd86-4593-8539-814c29caac51/partitions/31299a87-b895-4b13-91c0-788756ca5ff3/replicas/132790818155726834p/, LSN: 
9427, GlobalCommittedLsn: 9427, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 1001, RequestCharge: 1.24, ItemLSN: -1, SessionToken: -1#9427, UsingLocalLSN: False, TransportException: null, BELatencyMs: 1.004, ActivityId: 9ef3ec05-b381-48c8-bd4e-96a7cb764041, RetryAfterInMs: , 
TransportRequestTimeline: {\"requestTimeline\":[{\"event\": \"Created\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424439Z\", \"durationInMs\": 0.0115},{\"event\": \"ChannelAcquisitionStarted\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424554Z\", \"durationInMs\": 0.0114},{\"event\": \"Pipelined\", \"startTimeUtc\": \"2023-02-23T20:53:15.4424668Z\", \"durationInMs\": 0.1556},{\"event\": \"Transit Time\", \"startTimeUtc\": \"2023-02-23T20:53:15.4426224Z\", \"durationInMs\": 1.8731},{\"event\": \"Received\", \"startTimeUtc\": \"2023-02-23T20:53:15.4444955Z\", \"durationInMs\": 0.1408},{\"event\": \"Completed\", \"startTimeUtc\": \"2023-02-23T20:53:15.4446363Z\", \"durationInMs\": 0}],\"serviceEndpointStats\":{\"inflightRequests\":1,\"openConnections\":1},\"connectionStats\":{\"waitforConnectionInit\":\"False\",\"callsPendingReceive\":0,\"lastSendAttempt\":\"2023-02-23T20:35:31.6351618Z\",\"lastSend\":\"2023-02-23T20:35:31.6351618Z\",\"lastReceive\":\"2023-02-23T20:35:31.6351618Z\"},\"requestSizeInBytes\":551,\"requestBodySizeInBytes\":26,\"responseMetadataSizeInBytes\":186,\"responseBodySizeInBytes\":166};\r\n ResourceType: Document, OperationType: Create\r\n, SDK: Microsoft.Azure.Documents.Common/2.14.0"
}
--------------------------------------------------------------------------------

My code is as follows:

// load .env
err := godotenv.Load()
handle(err)

// create CosmosDB credentials
endpoint := os.Getenv("AZURE_COSMOS_ENDPOINT")
key := os.Getenv("AZURE_COSMOS_KEY")
cred, err := azcosmos.NewKeyCredential(key)
handle(err)

// create CosmosDB client
client, err := azcosmos.NewClientWithKey(endpoint, cred, nil)
handle(err)
log.Println("CosmosDB client has been successfully created...")

// create Container instance to perform read-write operations
container, err := client.NewContainer("vaporwave", "employees")
handle(err)
log.Println("Container has been successfully created...")

// generate a PartitionKey and example item
pk := azcosmos.NewPartitionKeyString("/_partitionKey")
item := map[string]string{
        "id":   "1",
        "value": "2",
}
marshalled, err := json.Marshal(item)
handle(err)

// create container item
itemResponse, err := container.CreateItem(context.Background(), pk, marshalled, nil)
if err != nil {
    var responseErr *azcore.ResponseError
    errors.As(err, &responseErr)
    log.Fatal(responseErr)
}
log.Printf("Item created. ActivityId %s consuming %v RU\n", itemResponse.ActivityID, itemResponse.RequestCharge)

This follows the pattern of creating an item in the azcosmos example test (see the ExampleContainerClient_CreateItem function). Screenshot of the container's partition key from the Azure portal. How am I continuing to get this error? It seems the PK values match to me, maybe I'm missing something.


Solution

  • Your problem is on line:

    pk := azcosmos.NewPartitionKeyString("/_partitionKey")
    

    This is the same scenario as this other question: https://stackoverflow.com/a/75536240/5641598

    A container has a Partition Key Definition/Path, which is set during the creation of the container and it's the JSON path to the property that will contain the Partition Key Value.

    If your path is /_partitionKey then:

    1. There should be a property in the document's body named _partitionKey
    2. Your code should be pk := azcosmos.NewPartitionKeyString("<the value of that property")

    In your case, your body has no _partitionKey property:

    item := map[string]string{
            "id":   "1",
            "value": "2",
    }
    

    So you would need to add it.

    Or re-evaluate if /_partitionKey is really the right Partition Key Definition/Path for your container/use case, maybe it is a different one. Whichever it is, the "item" operations, require the value.