首页 > 解决方案 > 如何使用 boto3 更新 dynamodb 中的密钥?

问题描述

我是 DynamoDB 的新手,在定义表时遇到了麻烦。我想要一张这样的桌子:

项目名称、创建日期、更新日期

我需要查询和更新具有特定 item_name 和 update_date 的项目。所以在 boto3 我写了

table.update_item(
            Key={
                'item_name': item_name,
                'update_date': 0
            },
            UpdateExpression='SET update_date = :val1',
            ExpressionAttributeValues={
                ':val1': 999999999
            }
        )

我明白了botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute update_date. This attribute is part of the key

在我的表定义中,我有item_name哈希键和update_date范围键。我将如何修复定义或代码,以便

  1. 更新操作成功。
  2. 我可以快速查询表中的item_nameupdate_date属性吗?

标签: amazon-dynamodbboto3

解决方案


1)首先updated_date是你主键的一部分,这意味着dynamodb中的主键由两个属性组成,Hash(item_name)和Range(updated_date)主键,你不能主键。

2)如果你想更新 updated_date 属性,那么你应该更新你的表配置并选择 item_name only 作为主键


推荐阅读