LoginSignup
33
25

More than 3 years have passed since last update.

AWS lambdaのDynamoDB更新処理で "Invalid UpdateExpression: Attribute name is a reserved keyword"になった時の対処方法

Last updated at Posted at 2019-08-30

lambdaからDynamoDBの値を更新しようとした時に
タイトルのエラーで怒られた時の対処方法です。

試しにこんな更新処理を書いてみます。

sample.py
table.update_item(
    Key= {'device_id': device_id},
    UpdateExpression='set store = :s, updated_at = :u',
    ExpressionAttributeValues={
        ':s' : store_name,
        ':u' : updated_at
    }
)

これを実行すると…


"An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: store"

どうやらstoreは予約語のようです。

こちらを参照
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/ReservedWords.html

対処方法として

ExpressionAttributeNamesを使用して、
属性名をプレースホルダー名で置き換えることで解決出来ました。

こんな感じです。

sample.py
table.update_item(
    Key= {'device_id': device_id},
    UpdateExpression='set #st = :s, updated_at = :u',
    ExpressionAttributeNames= {
        '#st' : 'store'
    },
    ExpressionAttributeValues={
        ':s' : store_name,
        ':u' : updated_at
    }
)

無事更新を行うことが出来ました。

参考

ここにハマった!DynamoDB
https://blog.brains-tech.co.jp/entry/2015/09/30/222148

DynamoDB の予約語
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/ReservedWords.html

33
25
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
33
25