Create a document in Cosmos DB via Logic App and PartitionKey mismatch in document & header

南楼画角 提交于 2021-02-07 19:18:34

问题


So I'm trying to create an Azure Logic App which will create a new record inside a Cosmos DB collection to store review results.

I've created a Cosmos DB database (called icecream) + collection (called reviews). The PartitionKey of the reviews collection is /flavorIdentifier.

In my Logic App I have a step which inserts a new document.

The code looks like this.

"Create_review": {
    "inputs": {
        "body": {
            "flavor": "@{body('JSON_parseren_2')?['flavor']}",
            "flavorIdentifier": "@{body('JSON_parseren')?['itemOrdered']}",
            "id": "@{guid()}",
            "review": "@{body('E-mail_met_opties_verzenden')?['SelectedOption']}"
        },
        "headers": {
            "x-ms-documentdb-raw-partitionkey": "@body('JSON_parseren')?['itemOrdered']"
        },
        "host": {
            "connection": {
                "name": "@parameters('$connections')['documentdb']['connectionId']"
            }
        },
        "method": "post",
        "path": "/dbs/@{encodeURIComponent('icecream')}/colls/@{encodeURIComponent('reviews')}/docs"
    },
    "runAfter": {
        "E-mail_met_opties_verzenden": [
            "Succeeded"
        ]
    },
    "type": "ApiConnection"
},

So, I'm posting the message to Cosmos DB

{
    "flavor": "My flavor",
    "flavorIdentifier": "3",
    "id": "4927042a-faa1-4e09-9c6d-d038ef659b25",
    "review": "Very satisfied"
}

As you can see, I also specified the partition key to the same value as flavorIdentifier. My guess is this should just work. But it doesn't

The error I'm receiving states

PartitionKey extracted from document doesn't match the one specified in the header

Which is strange, as both have the same value, also when checking the raw input and output. Is there anything I'm overlooking?

For completeness, the full input and output of this step.

The raw input of the failed step.

{
    "host": {
        "connection": {
            "name": "/subscriptions/3b3729b4-021a-48b5-a2eb-47be0c7e7f44/resourceGroups/developerday-workshop/providers/Microsoft.Web/connections/documentdb"
        }
    },
    "method": "post",
    "path": "/dbs/icecream/colls/reviews/docs",
    "headers": {
        "x-ms-documentdb-raw-partitionkey": "3"
    },
    "body": {
        "flavor": "My flavor",
        "flavorIdentifier": "3",
        "id": "4927042a-faa1-4e09-9c6d-d038ef659b25",
        "review": "Very satisfied"
    }
}

The raw output of the failed step.

{
    "statusCode": 400,
    "headers": {
        "x-ms-last-state-change-utc": "Wed,27 Mar 2019 05:03:54.568 GMT",
        "lsn": "1",
        "x-ms-schemaversion": "1.7",
        "x-ms-quorum-acked-lsn": "1",
        "x-ms-substatus": "1001",
        "x-ms-current-write-quorum": "3",
        "x-ms-current-replica-set-size": "4",
        "x-ms-xp-role": "1",
        "x-ms-global-Committed-lsn": "1",
        "x-ms-number-of-read-regions": "0",
        "x-ms-transport-request-id": "1",
        "x-ms-cosmos-llsn": "1",
        "x-ms-cosmos-quorum-acked-llsn": "1",
        "x-ms-session-token": "1",
        "x-ms-request-charge": "1.24",
        "x-ms-serviceversion": "version=2.2.0.0",
        "x-ms-activity-id": "bd428162-ec1a-4f50-879a-04b8ca0817a1",
        "Strict-Transport-Security": "max-age=31536000",
        "x-ms-gatewayversion": "version=2.2.0.0",
        "Timing-Allow-Origin": "*",
        "x-ms-apihub-cached-response": "false",
        "Date": "Wed, 27 Mar 2019 11:39:57 GMT",
        "Content-Length": "1149",
        "Content-Type": "application/json"
    },
    "body": {
        "code": "BadRequest",
        "message": "Message: {\"Errors\":[\"PartitionKey extracted from document doesn't match the one specified in the header\"]}\r\nActivityId: bd428162-ec1a-4f50-879a-04b8ca0817a1, Request URI: /apps/9bc5d0cc-9b7c-4b1d-9be2-0fa2654271c4/services/9d84c048-383b-498e-8472-1f57da72135d/partitions/0f29761a-9ffc-4560-94ce-0328c3c79f92/replicas/131981366274575376p/, RequestStats: \r\nRequestStartTime: 2019-03-27T11:39:57.0795308Z, RequestEndTime: 2019-03-27T11:39:57.0995601Z, Number of regions attempted: 1\r\nResponseTime: 2019-03-27T11:39:57.0995601Z, StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-westeurope1-fd21.documents.azure.com:16833/apps/9bc5d0cc-9b7c-4b1d-9be2-0fa2654271c4/services/9d84c048-383b-498e-8472-1f57da72135d/partitions/0f29761a-9ffc-4560-94ce-0328c3c79f92/replicas/131981366274575376p/, LSN: 1, GlobalCommittedLsn: 1, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 400, SubStatusCode: 1001, RequestCharge: 1.24, ItemLSN: -1, SessionToken: 1, UsingLocalLSN: False, TransportException: null, ResourceType: Document, OperationType: Create\r\n, SDK: Microsoft.Azure.Documents.Common/2.2.0.0"
    }
}

Any thoughts?


回答1:


Please add double quotation marks "" around your partition key and it works fine for me.

View the code:



来源:https://stackoverflow.com/questions/55376489/create-a-document-in-cosmos-db-via-logic-app-and-partitionkey-mismatch-in-docume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!