ardito.bryan
ardito.bryan

Reputation: 491

boto3, python: appending value to DynamoDB String Set

I have an object in DynamoDB:

{ 'UserID' : 'Hank', ConnectionList : {'con1', 'con2'} }

By using boto3 in lambda functions, I would like to add 'con3' to the String Set. So far, I have been trying with the following code without success:

ddbClient = boto3.resource('dynamodb')
table = ddbClient.Table("UserInfo")
table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "SET ConnectionList = list_append(ConnectionList, :i)",
    ExpressionAttributeValues = {
        ":i": { "S": "Something" }
    },
    ReturnValues="ALL_NEW"
)

However, no matter the way I try to put the information inside the String Set, it always runs error.

Upvotes: 1

Views: 930

Answers (1)

Maurice
Maurice

Reputation: 13197

Since you're using the resource API, you have to use the Python data type set in your statement:

table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "ADD ConnectionList :i",
    ExpressionAttributeValues = {
        ":i": {"Something"},  # needs to be a set type
    },
    ReturnValues="ALL_NEW"
)

Upvotes: 4

Related Questions