boto3发电机数据库PutItem带有保留字



我想把一些数据存储到具有这个主键的dynawdb表中

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

最新更新