AWS-Dynamo:UpdateItem会在Index GSI上引发错误

时间:2015-08-04 18:23:15

标签: amazon-dynamodb

我正在尝试更新像这样的元素

$response = $dynamo->updateItem(array(
    "TableName" => "posts",
    "IndexName" => "college_id-post_id-index",
    "Key" => array(
        "college_id" => array('S' => $collegeId),
        "post_id" => array('S' => $postId )
    ),
    "ExpressionAttributeValues" =>  array (
        ":reply" => $reply
    ),
    #"UpdateExpression" => "set reply = if_not_exists(reply , :reply)",
    "UpdateExpression" => "set reply = :reply",
    "ReturnValues" => 'ALL_NEW'
));


college_id-post_id-index is Global Secondary Index having hash key `college_id` and range key `post_id` . 

当我执行更新时,我收到错误The provided key element does not match the schema

任何人都可以帮助我解决问题吗?

1 个答案:

答案 0 :(得分:2)

如上所述,您能否分享基表的架构?

听起来您正在尝试直接更新表的GSI - 这在DynamoDB中是不可能的。在DynamoDB表上创建GSI时,DynamoDB会跟踪GSI并确保在基表项更新时自动更新。

在这种情况下,假设你有一个基表(我不知道架构,但我假设你有post_id作为哈希键,college_id作为范围键)。您可以尝试以下略微修改的代码:

$response = $dynamo->updateItem(array(
"TableName" => "posts",
"Key" => array(
    "post_id" => array('S' => $postId ),
    "college_id" => array('S' => $collegeId)
),
"ExpressionAttributeValues" =>  array (
    ":reply" => $reply
),
"UpdateExpression" => "set reply = :reply",
"ReturnValues" => 'ALL_NEW'
));

上述请求将更新基表中的相应项,但GSI也将更新以反映新值。

您可以阅读有关基表与GSI here之间数据同步的更多信息。