Tiffany
Tiffany

Reputation: 11

Adding an Attribute that does not exist into DynamoDb

If user enters an attribute that does not exist in Dynamodb, accept the attribute and store into Dynamodb.

Currently, I have 'id' and 'firstName' as the attributes and user can create by inserting their firstName however, if they want to add a 'lastName' attribute, dynamodb should accept the 'lastName' and store into the database.

def create(event):
    body = dict(event['body'])
    id = body['id']
    firstName = body['firstName']

    response = table.put_item(
        Item={
            'id': str(id),
            'firstName': firstName,
        },
    )

Upvotes: 0

Views: 3385

Answers (1)

notionquest
notionquest

Reputation: 39226

DynamoDB is a NoSQL database. This is the default feature in most of the NoSQL databases. This is supported in DynamoDB as well. Like RDBMS, the NoSQL database doesn't have any schema. You can add any non-key attributes anytime.

With respect to DynamoDB, while creating the table, it doesn't allow to define any non-key attributes. The lastName is a non-key attribute. It can be added to the item (i.e. record) at any time after you create the item (or) while creating the new item. This is one of the main advantages of NoSQL or DynamoDB database.

UpdateExpression = 'SET lastName = :lastNameVal',   
ExpressionAttributeValues={':lastNameVal' : 'John'}

Reference link

Upvotes: 2

Related Questions