Krishna
Krishna

Reputation: 1985

ConditionalCheckFailedException: The conditional request failed (Nested field)

I have a DDB entry as below

RequestId:'1234567890' // Partition key
LastScanDateTime: '2023-01-12T11:00:00.111Z'
SubjectUpdateCounter: {
  Maths: 1,
  English: 1
}

I'm trying to update the entry and below is the update query.

{
    "TableName": "EmpDetails",
    "Key": {
        "RequestId": {
            "S": "1234567890"
        }
    },
    "ConditionExpression": "(#subjectUpdateCounter > :subjectUpdateCounterLimit)",
    "UpdateExpression": "ADD #subjectUpdateCounter :dec  SET LastScanDateTime = :LastScanDateTime,",
    "ExpressionAttributeValues": {
        ":dec": {
            "N": "-1"
        },
        ":LastScanDateTime": {
            "S": "2023-02-13T18:14:52.143Z"
        },
        ":subjectUpdateCounterLimit": {
            "N": "0"
        }
    },
    "ReturnValues": "NONE",
    "ExpressionAttributeNames": {
        "#subjectUpdateCounter": "SubjectUpdateCounter.Maths"
    }
}

Getting below error

ConditionalCheckFailedException: The conditional request failed
  ....
  ....
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: '12345ygfsdfagagdf',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  __type: 'com.amazonaws.dynamodb.v20120810#ConditionalCheckFailedException'
}

My current value in SubjectUpdateCounter.Maths is greater than 0, so the condition should succeed and this query should decrement the value of SubjectUpdateCounter.Maths to 0.

Why is the query throwing the above exception?

Upvotes: 0

Views: 620

Answers (1)

Leeroy Hannigan
Leeroy Hannigan

Reputation: 19793

Your issue is here:

 "ExpressionAttributeNames": {
        "#subjectUpdateCounter": "SubjectUpdateCounter.Maths"
    }

This means DynamoDB is looking for an attribute named "SubjectUpdateCounter.Maths" but there is none, as its a nested value you are looking for.

Your request should look like the following:

{
    "TableName": "EmpDetails",
    "Key": {
        "RequestId": {
            "S": "1234567890"
        }
    },
    "ConditionExpression": "(#subjectUpdateCounter.#maths > :subjectUpdateCounterLimit)",
    "UpdateExpression": "ADD #subjectUpdateCounter.#maths :dec  SET LastScanDateTime = :LastScanDateTime,",
    "ExpressionAttributeValues": {
        ":dec": {
            "N": "-1"
        },
        ":LastScanDateTime": {
            "S": "2023-02-13T18:14:52.143Z"
        },
        ":subjectUpdateCounterLimit": {
            "N": "0"
        }
    },
    "ReturnValues": "NONE",
    "ExpressionAttributeNames": {
        "#subjectUpdateCounter": "SubjectUpdateCounter", 
        "#maths":"Maths"
    }
}

Upvotes: 1

Related Questions