I have the following DynamoDB table on AWS.
--Table name: client_id_master
--Partition key: client-id
(str)
--Attribute:
device-time-stamp
(int)This time, I created the following code to update the value of the device-time-stamp
column of the data on this table from Python (boto3) by the ʻupdate_item ()` method.
from datetime import datetime
import boto3
dynamodb = boto3.resource('dynamodb')
now_unix_time = datetime.now().strftime('%s')
clientId = 'ef5b728f4a74aed'
option = {
'Key': {'client-id': clientId},
'UpdateExpression': 'set #device-time-stamp = :timeStamp',
'ExpressionAttributeNames': {
'#device-time-stamp': 'device-time-stamp'
},
'ExpressionAttributeValues': {
':timeStamp': now_unix_time
}
}
table = dynamodb.Table('client_id_master')
table.update_item(**option)
However, when I ran this code, I got the following error:
An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeNames contains invalid key: Syntax error; key: \"#device-time-stamp\""
The cause is that there are restrictions on the characters that can be used in the variable name specified by ʻUpdateExpression of ʻupdate_item ()
, and this time -
included in # device-time-stamp
corresponds to the unusable character. There was a Syntax error.
Therefore, by setting the variable name to # device_time_stamp
as shown below, the code can be executed normally.
from datetime import datetime
import boto3
dynamodb = boto3.resource('dynamodb')
now_unix_time = datetime.now().strftime('%s')
clientId = 'ef5b728f4a74aed'
option = {
'Key': {'client-id': clientId},
'UpdateExpression': 'set #device_time_stamp = :timeStamp',
'ExpressionAttributeNames': {
'#device_time_stamp': 'device-time-stamp'
},
'ExpressionAttributeValues': {
':timeStamp': now_unix_time
}
}
table = dynamodb.Table('client_id_master')
table.update_item(**option)
that's all
Recommended Posts