如何重命名DynamoDB列/键 [英] How to rename DynamoDB column/key
问题描述
在我的DynamoDb表之一中,我有一个名为状态的列/键,事实证明这是一个保留关键字。不幸的是,不能删除整个表并重新启动它。如何重命名密钥?
In one of my DynamoDb tables I have a column/key named "status", which turned out to be a reserved keyword. Unfortunately it isn't an option to delete the whole table and reinitiate it. How can I rename the key?
以下是导致异常的Lambda代码:
Here is the Lambda Code that causes the Exception:
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)
这是例外:
2016-06-14 18:47:24 UTC + 2
ok错误:调用UpdateItem操作时发生错误(ValidationException):无效的UpdateExpression:属性名称是保留关键字;保留关键字:status
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
推荐答案
没有真正简单的方法来重命名列。您将必须为每个条目创建一个新属性,然后删除现有属性的所有值。
There is no real easy way to rename a column. You will have to create a new attribute for each of the entries and then delete all the values for the existing attribute.
没有理由删除您的属性/列,如果查询表时遇到问题,请使用表达式属性名称。
There is no reason to drop your attribute/column, if you are having trouble querying the table use Expression Attribute Names.
来自表达式属性名称文档:
在某些情况下,可能需要编写包含与DynamoDB保留字冲突的属性名称的表达式。要解决此问题,您可以定义一个表达式属性名称。 表达式属性名称是您在表达式中使用的占位符,代替了实际的属性名称。
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.
这篇关于如何重命名DynamoDB列/键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!