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
You cannot update the primary key attributes using UpdateItem. Instead, delete the item and use PutItem to create a new item with new attributes. The UpdateItem operation includes an Action parameter, which defines how to perform the update. You can put, delete, or add attribute values.
Primary key. When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.
There are two types of primary keys in DynamoDB: Partition key: This is a simple primary key. If the table has only a partition key, then no two items can have the same partition key value. Composite primary key: This is a combination of partition key and sort key.
To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With