Esben von Buchwald
Esben von Buchwald

Reputation: 3049

ConditionExpression in DynamoDB update operation gives unknown error

I'm running through a lot of items in a DynamoDB table and if the item contains a certain field (which is a object), I want to set some properties on the object on that field.

I've tried two variations of the code, but both of them give me the following unspecified error

{
    "errorType": "ConditionalCheckFailedException",
    "errorMessage": "The conditional request failed",
    "code": "ConditionalCheckFailedException",
    "message": "The conditional request failed",
    "time": "2020-08-29T11:44:11.703Z",
    "requestId": "IAN8A31FN2F1HR0ORT1RMTH5ENVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 26.45456917481479,
    "stack": [
        "ConditionalCheckFailedException: The conditional request failed",
        "    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
        "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
        "    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
        "    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
        "    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
        "    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)",
        "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
    ]
}

Here's the code with named attributes


          const itemParams = {
            TableName: process.env.vehiclesTableName,
            Key: { vehicleId },
            UpdateExpression:
              'set #learned.#versionId = :versionId, #learned.#fileFormat = :fileFormat',
            ExpressionAttributeValues: {
              ':versionId': VersionId,
              ':fileFormat': fileFormat,
            },
            ConditionExpression: 'attribute_exists(#learned)',
            ExpressionAttributeNames: {
              '#learned': 'learned',
              '#versionId': 'versionId',
              '#fileFormat': 'fileFormat',
            },
          }
          await db.update(itemParams).promise()



And without named attributes

          const itemParams = {
            TableName: process.env.vehiclesTableName,
            Key: { vehicleId },
            UpdateExpression:
              'set learned.versionId = :versionId, learned.fileFormat = :fileFormat',
            ExpressionAttributeValues: {
              ':versionId': VersionId,
              ':fileFormat': fileFormat,
            },
            ConditionExpression: 'attribute_exists(learned)',
          }
          await db.update(itemParams).promise()

They both behave the same.

Upvotes: 2

Views: 9407

Answers (1)

Dunedan
Dunedan

Reputation: 8435

The exception you're seeing is actually the expected behavior and indicates that the condition didn't evaluate to true. From the documentation:

ConditionalCheckFailedException

Message: The conditional request failed.

You specified a condition that evaluated to false. For example, you might have tried to perform a conditional update on an item, but the actual value of the attribute did not match the expected value in the condition.

As you're getting that ConditionalCheckFailedException exception, you're apparently trying to update an item which doesn't have a learned attribute.

Upvotes: 6

Related Questions