skip to Main Content

Following data mapped in DynamoDB for record_id 7, I want to update customer with new value

"customer": {
            "value": "id2",
            "label": "Customer2"
        }

However, dyanamoDB does not allow to update because of "ValidationException: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: value".

Record in DynamoDB:

{
    "terms": "Terms",
    "action": {
        "value": "id1",
        "label": "In Progress"
    },
    "line_items": [{
        "product": "dd",
        "quantity": "10",
    }],
    "_id": "7",
    "customer": {
        "value": "id1",
        "label": "Customer1"
    }
}


   updateExpression = "set "
   updateValues = dict()
   expression_attributes_names = {}

   for key, value in dictData.items():  
     key1 =  key.replace('.', '.#')
     updateExpression +=" #{key1} = :{key},"
     updateValues[f":{key}"] = value
     expression_attributes_names[f"#{key1}"] = key

   table.update_item(
   Key={
    'id': item_id
   },
   UpdateExpression=updateExpression,
   ExpressionAttributeValues=updateValues
   ExpressionAttributeNames=expression_attributes_names
   )

2

Answers


  1. it will be easier for me if you share the query expression you tried but in case of reserved words you need to rename the field in the query:

    table.update_item(
        Key={
            '_id': 7
        },
        UpdateExpression="set #val = :v",
        ExpressionAttributeValues={
            ":v": "false",
        },
        ExpressionAttributeNames={
          "#val": "value"
        }
    )
    
    Login or Signup to reply.
  2. UpdateExpression expects a string and you are passing a list:

    https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html#DDB-UpdateItem-request-UpdateExpression

    I would also suggest you just print the values and check that they match the correct syntax expected for the parameters you’re passing.

    For nested values you just follow the same concept:

    #customer.#value

    https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html#Expressions.ExpressionAttributeNames.NestedAttributes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search