How do I conditionally insert an item into a dynamodb table using boto3

后端 未结 3 696
無奈伤痛
無奈伤痛 2020-12-25 12:18

If I have a table with a hash key of userId and a range key of productId how do I put an item into that table only if it doesn\'t already exist using boto3\'s dynamodb bindi

3条回答
  •  悲哀的现实
    2020-12-25 13:15

    i think that you will get better documentation using client.put_item rather than table.put_item

    from boto3 documentation:

    To prevent a new item from replacing an existing item, use a conditional expression that contains the attribute_not_exists function with the name of the attribute being used as the partition key for the table. Since every record must contain that attribute, the attribute_not_exists function will only succeed if no matching item exists.

    ConditionExpression:

    ConditionExpression (string) -- A condition that must be satisfied in order for a conditional PutItem operation to succeed.

    An expression can contain any of the following:

    Functions: attribute_exists | attribute_not_exists | attribute_type | contains | begins_with | size These function names are case-sensitive.

    i am using dynamodb2 overwrite parameter on item.save()

提交回复
热议问题