简体   繁体   English

列出 append 不适用于 DynamoDB 中的 AWS boto3

[英]List append not working for AWS boto3 in DynamoDB

I'm trying to append a value to a DynamoDB table entry:我正在尝试将 append 值添加到 DynamoDB 表条目中:

{
 "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:以下是我的 python 代码:

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"

list_append works with 2 lists: list_append适用于 2 个列表:

list_append(list1, list2)

You need to pass the update expression along with the type of the element that you're adding ie "L" for a list:您需要传递更新表达式以及要添加的元素类型,即列表的“L”:

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.例如,请参阅文档

    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...这是正确的解决方案...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM