Pdog
Pdog

Reputation: 1

Trying to put_item() to DynamoDB only if the complete instance with all attribute values doesn't exist already

def put_items_db(data_dict):
"""
Put provided dictionary to lqtpid database
"""
try:
    response = self.table.put_item(Item=data_dict, ConditionExpression='attribute_not_exists(firstName)'
                                                                       ' AND attribute_not_exists(lastName)')
    http_code_response = response['ResponseMetadata']['HTTPStatusCode']
    logging.debug(f'http code response for db put {http_code_response}')

except ClientError as e:
# Ignore the ConditionalCheckFailedException
    if e.response['Error']['Code'] != 'ConditionalCheckFailedException':
        raise

When running the code it is still uploading entries that already exist...

Upvotes: 0

Views: 45

Answers (1)

karjan
karjan

Reputation: 1016

What are your keys for that table? I'm assuming you are putting an item that has a different keys than the item you're comparing to. With ConditionExpression you only compare the item you're writing to one item in table, the one with exactly same keys.

Upvotes: 1

Related Questions