ConditionalCheckFailedException: 条件请求失败(嵌套字段)

huangapple go评论66阅读模式
英文:

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?

英文:

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

得分: 1

你的问题在这里:

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

这意味着 DynamoDB 正在寻找一个名为 "SubjectUpdateCounter.Maths" 的属性,但实际上没有这个属性,因为你正在寻找一个嵌套的值。

你的请求应该如下所示:

{
    "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"
    }
}
英文:

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"
    }
}

huangapple
  • 本文由 发表于 2023年2月14日 02:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定