在我的一个 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/