python - 如何重命名 DynamoDB 列/键

标签 python amazon-web-services amazon-dynamodb boto3

在我的一个 DynamoDb 表中,我有一个名为“status”的列/键,结果证明它是一个保留关键字。不幸的是,不能选择删除整个表并重新启动它。如何重命名 key ?

这是导致异常的 Lambda 代码:

try :
            response = table.query(
                IndexName='myId-index',
                KeyConditionExpression=Key('myId').eq(someId)
            )
            for item in response['Items']:
                print('Updating Item: ' + item['id'])
                table.update_item(
                    Key={
                        'id': item['id']
                    },
                    UpdateExpression='SET myFirstKey = :val1, mySecondKey = :val2, myThirdKey = :val3, myFourthKey = :val4, myFifthKey = :val5, status = :val6',
                    ExpressionAttributeValues={
                        ':val1': someValue1,
                        ':val2': someValue2,
                        ':val3': someValue3,
                        ':val4': someValue4,
                        ':val5': someValue5,
                        ':val6': someValue6
                    }
                )
except Exception, e:
            print ('ok error: %s' % e)

这里是异常(exception):

2016-06-14 18:47:24 UTC+2 ok error: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: status

最佳答案

没有真正简单的方法来重命名列。您必须为每个条目创建一个新属性,然后删除现有属性的所有值。

没有理由删除您的属性/列,如果您在查询表时遇到问题,请使用表达式属性名称

来自Expression Attribute Names文档:

On some occasions, you might need to write an expression containing an attribute name that conflicts with a DynamoDB reserved word... To work around this, you can define an expression attribute name. An expression attribute name is a placeholder that you use in the expression, as an alternative to the actual attribute name.

关于python - 如何重命名 DynamoDB 列/键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37817879/

相关文章:

Python:循环中的产量与打印

java - 使用 Java 查询 DynamoDB 中的最大属性值

azure - 从VM内部如何识别它是AWS机器还是Azure机器?

node.js - 如何修复数据未插入本地 dynamodb (NodeJS)

python - 远大前程 : base_directory must be an absolute path if root_directory is not provided

python - 在 docker 容器中运行 python 脚本时导入错误?

python - Pandas 数据框 : swap column headings by index

postgresql - 在 AWS 中运行脚本?

amazon-dynamodb - 更新时使用的 DynamoDB 写入容量

go - 查询不是 DynamoDb 上的索引的参数