20
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DynamoDBのupdateItemのValidationException解消方法

Last updated at Posted at 2017-02-21

#AWSマネジメントコンソールでDynamoDBのテーブル作成する場合

スクリーンショット 2017-02-21 18.20.12.png

#idはプライマリキーパーティションキーになっています。下記のソースをupdateItemすると

let updateParam = {
                        "TableName" : "test",
                        "Key" : {
                            "id" : {"S" : "12345"}
                        },
                        "UpdateExpression" : "SET #name =:name",
                        "ExpressionAttributeNames" : {
                            "#name" : "name"
                        },
                        "ExpressionAttributeValues" : {
                            ":name" : {"S" : "Jane"}
                        }
                    };
dynamodb.updateItem(updateParam, function(error, data) {
        if(error) {
            logger.error('error=' + util.inspect(error, false, null));
            if (error.retryable) {
                 //retry 
                 return;
            } else {
                //todo
                return;
            }
        } else {
            logger.info('update succeed');
        }
    });

##ValidationException
下記のvalidationエラーが出ます。

ValidationException: The provided key element does not match the schema

##原因
AWSのドキュメントをみたら

You can query only tables whose primary key is of hash-and-range type

マネジメントコンソールから作成されたテーブルのプライマリキーはシンプル (パーティションキー) または複合 (パーティションキーとソートキー) になっています。UpdateItemはサポートされたプライマリキーはハッシュかハッシュ&レンジです。

##解決方法
コマンドでテーブル作成、プライマリキーのtypeを指定することです。

aws dynamodb create-table --table-name test --attribute-definitions '[{"AttributeName": "id", "AttributeType": "S"}]' --key-schema '[{"AttributeName": "id", "KeyType": "HASH"}]' --provisioned-throughput '{"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}'

もう一回上記のnodeを実行したら、正常に更新できました。

[2017-02-21 16:00:06.062] [INFO] [default] - update succeed
[2017-02-21 16:00:06.063] [INFO] [default] - update succeed
[2017-02-21 16:00:06.063] [INFO] [default] - update succeed
[2017-02-21 16:00:06.065] [INFO] [default] - update succeed
[2017-02-21 16:00:06.065] [INFO] [default] - update succeed
[2017-02-21 16:00:06.083] [INFO] [default] - update succeed
[2017-02-21 16:00:06.084] [INFO] [default] - update succeed
[2017-02-21 16:00:06.085] [INFO] [default] - update succeed
[2017-02-21 16:00:06.087] [INFO] [default] - update succeed
20
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?