我正在尝试更新 DynamoDB 表中的项目。我编写的代码正在更新该项目,但是当我添加标题为“源/目标”的列时,它给出了“ValidationException”异常。
用于更新的代码 -
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")
response = table.update_item(
Key={
'id': "test_id
},
UpdateExpression="set source/target= :st, user_name= :usr",
ExpressionAttributeValues={
':st' : event['source/target'],
':usr' : event['user_name']
},
ReturnValues="UPDATED_NEW"
)
我得到的错误是 -
An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: \"/\", near: \"source/target\""
如何解决?
最佳答案
您好,当您有以下任何特殊字符时,您必须使用 ExpressionAttributeNames
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")
response = table.update_item(
Key={
'id': "test_id
},
UpdateExpression="set #colName= :st, user_name= :usr",
ExpressionAttributeValues={
':st' : event['source/target'],
':usr' : event['user_name']
},
ExpressionAttributeNames={
'#colName' : 'source/target'
},
ReturnValues="UPDATED_NEW"
)
关于python - 更新 dynamoDB 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55256127/