簡體   English   中英

列出 append 不適用於 DynamoDB 中的 AWS boto3

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

我正在嘗試將 append 值添加到 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"

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

例如,請參閱文檔

    response = usersTable.update_item(
    Key={
        'id': userid,
    },
    UpdateExpression="set history=list_append(history,:isbn)",
    ExpressionAttributeValues={ 
        ':isbn': [bookisbn]

    },
    ReturnValues="UPDATED_NEW"
)

這是正確的解決方案...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM