简体   繁体   English

带有保留字的 boto3 dynamodb PutItem

[英]boto3 dynamodb PutItem with reserved word

i want to store some data to dynamodb table which has this primary key我想将一些数据存储到具有此主键的 dynamodb 表中

partition key: invocationId
sort key: index

at the moment of using boto3 to PutItem the following error show up:在使用 boto3 到 PutItem 时出现以下错误:

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

and indeed, index is part of reserved words list:( https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html事实上, index是保留字列表的一部分:( https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

i tried the following code using ExpressionAttributeNames :我使用ExpressionAttributeNames尝试了以下代码:

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

but it raises:但它提出了:

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

it's worth to mention that .net client works ok for the same use case, because i have put some records with index as sort key in that same table, but now it is required to use this table from python and boto3.值得一提的是,.net 客户端适用于相同的用例,因为我在同一个表中放置了一些index作为排序键的记录,但现在需要使用 python 和 boto3 中的此表。

this is how it works in .net这就是它在 .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();

after this snippet execution, index column is populated correctly, so after all.执行此代码段后,索引列被正确填充,所以毕竟。 it is possible.有可能的。

You used the right solution - ExpressionAttributeNames - but in the wrong way.您使用了正确的解决方案 - ExpressionAttributeNames - 但使用了错误的方式。

When you define the "Item" to insert, you need to still use the full attribute name "index" - that's fine:当您定义要插入的“项目”时,您仍然需要使用完整的属性名称“索引” - 这很好:

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

The word "index" in the item's definition was never the problem - the problem was (as the error message told you) that you used this word in an ConditionExpression .项目定义中的“索引”一词从来都不是问题 - 问题是(正如错误消息告诉您的那样)您在ConditionExpression中使用了这个词。 So you need to use this #I inside your ConditionExpression , and also pass ExpressionAttributeNames exactly like you did.所以你需要在你的ConditionExpression中使用这个#I ,并且像你一样传递ExpressionAttributeNames

If you don't have a ConditionExpression (or some other expression) in your request, you don't need the #I , and not allowed to pass the unused ExpressionAttributeNames .如果您的请求中没有ConditionExpression (或其他表达式),则不需要#I ,并且不允许传递未使用的ExpressionAttributeNames

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

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