【问题标题】:List append not working for AWS boto3 in DynamoDB列表追加不适用于 DynamoDB 中的 AWS boto3
【发布时间】:2021-08-11 08:38:35
【问题描述】:

我正在尝试将值附加到 DynamoDB 表条目:

{
 "id": "1",
 "history": [
  "638380895-8"
 ],
 "currentbooks": "",
 "email": "ocomford0@craigslist.org",
 "name": "Osborne Comford"
}

我正在尝试编写代码以将字符串条目添加到“历史”数组。

以下是我的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"
)

它给了我错误:

"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"

【问题讨论】:

    标签: python amazon-web-services aws-lambda amazon-dynamodb


    【解决方案1】:

    list_append 适用于 2 个列表:

    list_append(list1, list2)
    

    您需要传递更新表达式以及要添加的元素的类型,即列表的“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"
    )
    

    docs 为例。

    【讨论】:

    • 已更新。再试一次。
    • 仍然给我错误:“errorMessage”:“调用UpdateItem操作时发生错误(ValidationException):无效的UpdateExpression:运算符或函数的操作数类型不正确;运算符或函数:list_append,操作数类型: M",
    • 忘记了 isbn 本身的类型。已更新。
    【解决方案2】:
        response = usersTable.update_item(
        Key={
            'id': userid,
        },
        UpdateExpression="set history=list_append(history,:isbn)",
        ExpressionAttributeValues={ 
            ':isbn': [bookisbn]
    
        },
        ReturnValues="UPDATED_NEW"
    )
    

    这是正确的解决方案...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2023-01-25
      • 2021-06-23
      相关资源
      最近更新 更多