Reputation: 445
I'm trying to append a value to a DynamoDB table entry:
{
"id": "1",
"history": [
"638380895-8"
],
"currentbooks": "",
"email": "ocomford0@craigslist.org",
"name": "Osborne Comford"
}
I'm trying to write the code to add a string entry to 'history' array.
Following is my python code:
usersTable = dynamodb.Table("users")
booksTable = dynamodb.Table("books")
bookisbn = event['isbn']
response = usersTable.update_item(
Key={
'id': userid,
},
UpdateExpression="set history=list_append(history, :s)",
ExpressionAttributeValues={
':s': bookisbn
},
ReturnValues="UPDATED_NEW"
)
It's giving me the error:
"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: S"
Upvotes: 0
Views: 1041
Reputation: 445
response = usersTable.update_item(
Key={
'id': userid,
},
UpdateExpression="set history=list_append(history,:isbn)",
ExpressionAttributeValues={
':isbn': [bookisbn]
},
ReturnValues="UPDATED_NEW"
)
This is the correct solution...
Upvotes: 0
Reputation: 21295
list_append
works with 2 lists:
list_append(list1, list2)
You need to pass the update expression along with the type of the element that you're adding i.e. "L" for a list:
response = usersTable.update_item(
Key={
'id': userid,
},
UpdateExpression="set history=list_append(history, :isbn)",
ExpressionAttributeValues={
':isbn': {"L": [ { "S" : isbn } ]} # ensure you pass a list
},
ReturnValues="UPDATED_NEW"
)
See the docs for example.
Upvotes: 3