Reputation: 737
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?
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)
And here is the 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
Upvotes: 12
Views: 28343
Reputation: 357
Using the NoSQL Workbench app AWS supports I exported a copy of the table which had the column name(s) I wanted to change. I opened the JSON file NoSQL Workbench created then did a simple Find/Replace for the name of the column in question.
With the names looking correct on the .JSON file I re-imported the table back into dynamodb using the NoSQL app. The import overwrites existing data which will wipe out the bad column name.
If you have a HUGE data set, downloading a copy to your local computer may not be a good solution, but for my small table it worked pretty well.
Upvotes: 1
Reputation: 71
There is a simple solution instead of renaiming a column: Use projection-expression and expression-attribute-names in your query.
I run over the same problem (my table contains the column "name". Here ist a sample query:
TableName: 'xxxxxxxxxx',
ExpressionAttributeNames: {
'#A': 'country',
'#B': 'postcode',
'#C': 'name'
},
ExpressionAttributeValues: {
':a': {S: 'DE'},
':c': {S: 'Peter Benz'}
},
FilterExpression: 'country = :a AND #C = :c',
ProjectionExpression: '#A, #B, #C'
Upvotes: 5
Reputation: 8401
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.
From the Expression Attribute Names documentation:
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.
Upvotes: 14