簡體   English   中英

帶有保留字的 boto3 dynamodb PutItem

[英]boto3 dynamodb PutItem with reserved word

我想將一些數據存儲到具有此主鍵的 dynamodb 表中

partition key: invocationId
sort key: index

在使用 boto3 到 PutItem 時出現以下錯誤:

An error occurred (ValidationException) when calling the PutItem operation: Invalid ConditionExpression: Attribute name is a reserved keyword; reserved keyword: index

事實上, index是保留字列表的一部分:( https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

我使用ExpressionAttributeNames嘗試了以下代碼:

self._table.put_item(
    Item={
        '#I': entity.index,
        'invocationId': entity.invocationId,
        ...
    },
    ExpressionAttributeNames={
        '#I': 'index'
    },
)

但它提出了:

An error occurred (ValidationException) when calling the PutItem operation: ExpressionAttributeNames can only be specified when using expressions

值得一提的是,.net 客戶端適用於相同的用例,因為我在同一個表中放置了一些index作為排序鍵的記錄,但現在需要使用 python 和 boto3 中的此表。

這就是它在 .net 中的工作方式

AmazonDynamoDBClient DynamoClient = new AmazonDynamoDBClient();
var _table = Table.LoadTable(DynamoClient, "my-table-name");
var d = new Document()
            {
                ["invocationId"] = "...",
                ["index"] = "...",
            };
d.Add("newParam", "value");
d.Add(...);
_table.UpdateItemAsync(d).GetAwaiter().GetResult();

執行此代碼段后,索引列被正確填充,所以畢竟。 有可能的。

您使用了正確的解決方案 - ExpressionAttributeNames - 但使用了錯誤的方式。

當您定義要插入的“項目”時,您仍然需要使用完整的屬性名稱“索引” - 這很好:

 Item={
        'index': entity.index,
        'invocationId': entity.invocationId,
        ...
    },

項目定義中的“索引”一詞從來都不是問題 - 問題是(正如錯誤消息告訴您的那樣)您在ConditionExpression中使用了這個詞。 所以你需要在你的ConditionExpression中使用這個#I ,並且像你一樣傳遞ExpressionAttributeNames

如果您的請求中沒有ConditionExpression (或其他表達式),則不需要#I ,並且不允許傳遞未使用的ExpressionAttributeNames

暫無
暫無

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

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