使用 update_item 的 boto3 资源在 dynamo DB 中添加新列

adding new column in dynamo DB using boto3 resource with update_item

我需要在 Dynomodb 中更新一行,同时需要包含一个已经不存在的新列。

resp = table.update_item(
            Key={
                'Pkey': 'key1',
                'Skey': 'skwy2'
            },
            UpdateExpression='ADD dateModified = :input2, SET IsActive = :input1',
            ExpressionAttributeValues={
                ':input1': False,
                ':input2' :  datetime.datetime.now(timezone.utc)
            },
            ReturnValues="UPDATED_NEW"
        )

现在我需要将 IsActive 字段更新为 false 并向其插入新的 dateModified 值。

出现错误 无效的更新表达式:语法错误;令牌:“=”,附近:“dateModified =:input2”

您可以在 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.ADD 中看到对于 ADD 操作您不使用 =

所以正确的表达应该是ADD dateModified :input2 SET IsActive = :input1