When I try to update the data on DynamoDB with Python (boto3), I get "ExpressionAttributeNames contains invalid key: Syntax error; key: <key name>"

Event

I have the following DynamoDB table on AWS.

--Table name: client_id_master --Partition key: client-id (str) --Attribute:

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

Cause / Solution

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)

image.png

that's all

Recommended Posts

When I try to update the data on DynamoDB with Python (boto3), I get "ExpressionAttributeNames contains invalid key: Syntax error; key: <key name>"
When I try to import pandas on macOS I get the error No module named'_bz2'
I get an error when I try to raise Python to 3 series using pyenv on Catalina
I tried to get CloudWatch data with Python
How to get the key on Amazon S3 with Boto 3, implementation example, notes
When I try to install mysqlclient with Django, I get error: command'gcc' failed with exit status 1.
When I try to connect django and postgresql with Docker, I get the error "django.db.utils.OperationalError: could not translate host name" db "to address: Name or service not known"
I get [Error 2055] when trying to connect to MySQL on Heroku
When I try to connect to MySQL with mysql-connector-python, I cannot connect with the error "SSL connection error: SSL_CTX_set_tmp_dh failed"
Try to decipher the garbled attachment file name with Python
[Introduction to Python] How to get data with the listdir function
I want to improve efficiency with Python even in an experimental system (4) Use ser.close () when an error is thrown using try syntax
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
When I get an error with Pylint in Atom on Windows
I get an error when trying to install maec 4.0.1.0 with pip
I tried to get started with Bitcoin Systre on the weekend
Try to solve the shortest path with Python + NetworkX + social data
Try to get CloudWatch metrics with re: dash python data source
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
When I import TensorFlow to Python, I get "Import Error: DLL load failed: The specified module cannot be found."
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Try to image the elevation data of the Geographical Survey Institute with Python
How to deal with SSL error when connecting to S3 with boto of Python
I tried to get the authentication code of Qiita API with Python.
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I get a Python No module named'encodings' error with the aws command
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to get the movie information of TMDb API with Python
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Try to poke DB on IBM i with python + JDBC using JayDeBeApi
[Python] Get the variable name with str
When I get an error with PyInstaller
I'm an amateur on the 14th day of python, but I want to try machine learning with scikit-learn
python I don't know how to get the printer name that I usually use.
[Python] I want to know the variables in the function when an error occurs!
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
I stumbled on the character code when converting CSV to JSON in Python
What to do if you get an error when installing python with pyenv
[Python] I tried to get the type name as a string from the type function
When I try to use Jupiter notebook on Mac, I can only select python2
I want to get the file name, line number, and function name in Python 3.4
[Introduction to Python] How to get the index of data with a for statement
I tried to save the data with discord
Try to solve the man-machine chart with Python
Introduction to Python with Atom (on the way)
When Attribute Error: Can't get attribute'class name' on <module'__main__' (built-in)> appears when loading python pickle
What to do if you get an OpenSSL error when installing Python 2 with pyenv
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
I tried to automate the article update of Livedoor blog with Python and selenium.
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
How to get the variable name itself in python
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
Try to solve the internship assignment problem with Python
I tried to touch the CSV file with Python