0

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?

1 Answer 1

1

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"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.