简体   繁体   English

Dynamodb put_item()覆盖数据

[英]Dynamodb put_item() overwrites data

Device is my Partition key, table is for putting in multiple different users, under the same Device. 设备是我的分区键,表用于在同一设备下放置多个不同的用户。 However, if I run the following put_item() code, it will overwrite each user if they have the same Device key. 但是,如果我运行以下put_item()代码,如果每个用户具有相同的设备密钥,它将覆盖每个用户。

Example: If I put in Monitor , as my device variable, and gomez as my aliasInput variable it runs. 示例:如果将Monitor作为device变量,将gomez作为我的aliasInput变量,它将运行。 Then run it again as Monitor again as my device variable, but craig as my aliasInput it overwrites my gomez entry. 然后再次以Monitor作为我的device变量来运行它,但是以craig作为我的aliasInput它将覆盖我的gomez条目。

function to input data into my table : 将数据输入到我的表的功能:

import boto3
import json
import decimal
import time
import datetime

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)


dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")

table = dynamodb.Table('WishListTest')

device = input('What is the Item being requested?\n')
device = device.upper()

aliasInput = input('What is the Alias of the user?\n')
aliasInput = aliasInput.upper()

date = int((time.strftime("%d%m%Y")))
response = table.put_item(
   Item={
        'Device': device,
        'RequestList': {
            'Alias': aliasInput,
            'Date': date
        },
        'AvailableQuanity': 0,
        'ReserveQuanity': 0,

    }
)

print("PutItem succeeded:")
print(json.dumps(response, 

You're looking for update_item() . 您正在寻找update_item() You should use UpdateExpression because AttributeUpdates is deprecated, but this simple example should get you started: 您应该使用UpdateExpression因为AttributeUpdates已过时,但是这个简单的示例应该使您入门:

response = table.update_item(
   Key={
     'Device': device,
   },
   AttributeUpdates={
     'RequestList': {
       'Alias': aliasInput,
       'Date': date
     },
     'AvailableQuanity': 0,
     'ReserveQuanity': 0,
   },
)

From the docs: Put item 文档中: 放置项

Creates a new item, or replaces an old item with a new item. 创建一个新项目,或用一个新项目替换一个旧项目。 If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item. 如果指定表中已经存在与新项目具有相同主键的项目,则新项目将完全替换现有项目。

To prevent an overwrite, you need to add a conditional expression specifying that the partition key does not yet exist. 为防止覆盖,您需要添加一个条件表达式 ,以指定分区键尚不存在。

Something like the following should work (sorry, I didn't quite get your key scheme, so you'll have to modify this). 像下面这样的东西应该起作用(对不起,我还不太了解您的密钥方案,因此您必须对此进行修改)。

table.put_item(
    Item={'userId': 1, 'productId': 2}, 
    ConditionExpression='userId <> :uid AND productId <> :pid', 
    ExpressionAttributeValues={':uid': 1, ':pid': 3}
)

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

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