python - 更新 dynamoDB 中的项目

标签 python aws-lambda amazon-dynamodb validationexception

我正在尝试更新 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/

相关文章:

python - 如何使用python获取目录中的文件名

python - 使用 Python 连接到 AWS Elasticache Redis 集群

amazon-web-services - 客户端错误: An error occurred (AccessDeniedException)

python - 使用 Django (Python) 在 DynamoDB 中自动生成 key

python - 无法使用 python boto sdk 连接 amazon DynamoDb Local

amazon-web-services - 使用 CloudFormation 创建 DynamoDB 表时为什么会收到 "AttributeName cannot be empty"?

python - 使用 matplotlib.animate 在 python 中对等高线图进行动画处理

python - 导入错误: cannot import name 'Affiliate' from partially initialized module 'affiliate.models' (most likely due to a circular import)

python - Pandas - 附加到 DataFrame 时如何控制列顺序

aws-lambda - 如何使用无服务器框架在 Lambda 中管理 Aurora Serverless 数据 API 的 typeORM 连接