0

I'm trying to append a value to a DynamoDB table entry:

{
 "id": "1",
 "history": [
  "638380895-8"
 ],
 "currentbooks": "",
 "email": "[email protected]",
 "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"

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Updated. Try again.
Still 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: M",
Forgot about the type for isbn itself. Updated.
0
    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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.