0

I have dynamodb

  • groupId is partition key
  • empdetails is list
  • dataDetails is the attribute name which i need to update
  • if list is not there it has to create, but if its there then I need update the dictionary

I need to update the dataDetails list of dictionary for new dictionaries if and only if 'empId': 1001 not in the dataDetails

Code is below

new_obj = {'groupId':101, 'empId': 1001, 'empName': 'Joe'}

In my dataDetails list [{'empId': 1002, 'empName': 'Fra'}] is already there

So current dataDetails list is [ {'empId': 1002, 'empName': 'Fra'}, {'empId': 1001, 'empName': 'Joe'}]

dynamodb_resource = boto3.resource('dynamodb')
table = dynamodb_resource.Table('myTable')
groupId = new_object['groupId']
        data_details = {"empId": new_object['empId'],
                        "empName": new_object['empName']}
        result = table.update_item(
            Key={
                'groupId': groupId ,
            },
            UpdateExpression=
            'SET dataDetails = list_append(if_not_exists(dataDetails, :empty_list), :data_details)',
            ExpressionAttributeValues={
                ':data_details': [data_details], ':empty_list': []
            },
            ReturnValues="UPDATED_NEW"
        )

I got an error "errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: :empty_list",

After lot of trials I got solution [data_details], it should be in list. Updating in the above Code for reference

Is there any conditional check I can do like if 'empId': 1001 is already in the dataDetails so that it should not append to the dictionary?

sim
  • 524
  • 3
  • 14
  • list_append(if_not_exists(dataDetails, ":empty_list":[]), :data_details)', see answers here, https://stackoverflow.com/questions/34951043/is-it-possible-to-combine-if-not-exists-and-list-append-in-update-item – MZM Dec 15 '22 at 04:10
  • @MZM I tried that, didnt worked – sim Dec 15 '22 at 04:13
  • If you're going to refer to `:empty_list` in your `UpdateExpression` then your `ExpressionAttributeValues` needs to define it. – hunterhacker Dec 15 '22 at 04:16
  • @hunterhacker can you update ExpressionAttributeValues in comment, i will try – sim Dec 15 '22 at 04:17
  • @hunterhacker ` ExpressionAttributeValues={ ":data_details": data_details, ":empty_list":[]},` I did this but still same error – sim Dec 15 '22 at 04:33
  • @hunterhacker Thanks at last i got the solution and I updated . Can you tell me the answer Is there any conditional check I can do like if 'empId': 1002 is already in the dataDetails so that it should not append to the dictionary? – sim Dec 15 '22 at 06:08

1 Answers1

1

There is more than the exception you are seeing wrong with your solution, but let's use 1 question on stackoverflow per problem as per the rules. The current exception is because you are using a variable that you have not declared, you can declare it like so:

dynamodb_resource = boto3.resource('dynamodb')
table = dynamodb_resource.Table('myTable')
groupId = new_object['groupId']
        data_details = {"empId": new_object['empId'],
                        "empName": new_object['empName']}
        result = table.update_item(
            Key={
                'groupId': groupId ,
            },
            UpdateExpression=
            'SET dataDetails = list_append(if_not_exists(dataDetails, :empty_list), :data_details)',
            ExpressionAttributeValues={
                ':data_details': [data_details], 
                ':empty_list': []
            },
            ReturnValues="UPDATED_NEW"
        )
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31