How to rename DynamoDB column/key

15,879

Solution 1

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.

Solution 2

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'
Share:
15,879

Related videos on Youtube

weka1
Author by

weka1

Updated on September 14, 2022

Comments

  • weka1
    weka1 over 1 year

    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

  • weka1
    weka1 almost 8 years
    Thank you for the quick response. The problem is not querying the data. It is that i can not access the data in the problem-keys programmatically since lambda will give me a keyword error then. So there is also no way to just copy the values to a new key and clearing the old one. :(
  • Shibashis
    Shibashis almost 8 years
    Are you retrieving data using lambda? or are u using dynamo db streams?
  • weka1
    weka1 almost 8 years
    I'm not quite sure, what you mean. The data goes into the dynamodb via lambda triggered by s3. I fetch it into my app via the iOS sdk and i'm trying to update various other tables, in case values in this table change, by lambda. But lambda crashes/throws an exception if i try to read/write the values, which are stored in keys which have the same name as a dynamodb keyword.
  • Shibashis
    Shibashis almost 8 years
    Got it, can you share the code snippet where it throws the exception?
  • weka1
    weka1 almost 8 years
    Thank you, i added the code to the original question!
  • weka1
    weka1 almost 8 years
    It took me a while, but you were right all along. Adding Expression Attribute Names to the update_item expression did make it work. Thank you!
  • jcollum
    jcollum almost 2 years
    I don't think there's a way to export more than 100 items from a table in that app though