0

How do I put an item in a dynamodb table only if that item does not exist. So far, I have:

table.put_item(
   Item:={
       'greeting': 'hi'
   },
   ConditionExpression= 'attribute_not_exists(hi)'
)

What would my condition expression be?

1 Answer 1

1

It would be something like this:

ConditionExpression= "attribute_not_exists(Id)" // where id is your unique key
Sign up to request clarification or add additional context in comments.

3 Comments

I just edited my question, however it is still not working. The item keeps getting overwritten,
You should replace hi in attribute_not_exists to the name of your partition key attribute e.g. attribute_not_exists(greeting) assuming "greeting" is the name of the partition key
You can catch the error with ClientError defined in the Boto3 SDK (See how), then add your error handling logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.