I have dynamodb
groupId
is partition keyempdetails
is listdataDetails
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?