9

I am using Amazonica, a Clojure library to write to DynamoDB.

The following inserts an item into DynamoDB and updates its content if called a second time, which is expected.

(ddb/put-item cred
   :table-name table-name
   :item payload)

Now, the following inserts an item only the first time. Calling it a second time doesn't do anything, which is what I need.

(ddb/put-item cred
   :table-name table-name
   :condition-expression "attribute_not_exists(clientId)"
   :item payload)

However with the latest I am getting an error:

The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException;

... which doesn't really make the code deliverable. My CloudFormation template is very simple:

"Resources": {
  "ClientTable": {
    "Type": "AWS::DynamoDB::Table",
    "Properties": {
      "AttributeDefinitions": [
        { "AttributeName": "clientId", "AttributeType": "S" }
      ],
      "KeySchema": [
        { "AttributeName": "clientId", "KeyType": "HASH" }
      ],
      "ProvisionedThroughput": {
        "ReadCapacityUnits": { "Ref": "ReadCapacityUnits" },
        "WriteCapacityUnits": { "Ref": "WriteCapacityUnits" }
      },
      "TableName": "ClientTable"
    }
  }
}

Am I missing something?

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130

1 Answers1

11

Amazonica is not silently swallowing the fact that the item was not added. It's throwing an exception that you can catch in case you want to do something about it. Perhaps you want to:

  1. catch the exception
  2. make sure it's error code matches that one
  3. ignore it if it does.

To test this, make sure that the same call does indeed work for new keys.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • Like to share .. I faced the same issue in AWS web console. The reason is that there are more than one items found for my search criteria and I updated one of the items. After changing the query to return my intended unique row, my update on that item/row got succeeded – Satheesh K May 01 '20 at 13:22