Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction failing when updating entity with unique item and not updating unique value #345

Open
brendanclement opened this issue May 26, 2023 · 1 comment

Comments

@brendanclement
Copy link

We have a case where were updating an entity Foo in a transaction that has a unique attribute "name". In this update transaction, we are not updating the "unique" attribute.

We are receiving the following error from dynamodb:

“message”:“Unable to edit task due to error: ValidationException: Transaction request cannot include multiple operations on one item”

upon turning on typedorm:* DEBUG logging - we see that two new writes are being added to the transaction, a PUT and a DELETE of the same autogenerated id value (I presume this is what's being used to calculate uniqueness) - dynamodb doesn't like this because we're trying to do two operations on the same key in the same transaction.

{
    "Put": {
      "TableName": "<TABLE_NAME>",
      "Item": {
        "pk": "DRM_GEN_<VALUE_OF_NAME_ATTRIBUTE>",
        "sk": "DRM_GEN_<VALUE_OF_NAME_ATTRIBUTE>"
      },
      "ConditionExpression": "(attribute_not_exists(#CE_pk)) AND (attribute_not_exists(#CE_sk))",
      "ExpressionAttributeNames": {
        "#CE_pk": "pk",
        "#CE_sk": "sk"
      }
    }
  },
  {
    "Delete": {
      "TableName": "<TABLE_NAME>",
      "Key": {
        "pk": "DRM_GEN_<VALUE_OF_NAME_ATTRIBUTE>",
        "sk": "DRM_GEN_<VALUE_OF_NAME_ATTRIBUTE>"
      }
    }
  }

I think this is happening because in the update, we're not changing the "name" attribute of Foo - if we do change the "name" attribute, the transaction succeeds because we'll preform a DELETE on the old unique key and a PUT on on the new unique key

{
    "Put": {
      "TableName": "<TABLE_NAME>",
      "Item": {
        "pk": "DRM_GEN_<NEW_VALUE_OF_NAME_ATTRIBUTE>",
        "sk": "DRM_GEN_<NEW_VALUE_OF_NAME_ATTRIBUTE>"
      },
      "ConditionExpression": "(attribute_not_exists(#CE_pk)) AND (attribute_not_exists(#CE_sk))",
      "ExpressionAttributeNames": {
        "#CE_pk": "pk",
        "#CE_sk": "sk"
      }
    }
  },
  {
    "Delete": {
      "TableName": "<TABLE_NAME>",
      "Key": {
        "pk": "DRM_GEN_<OLD_VALUE_OF_NAME_ATTRIBUTE>",
        "sk": "DRM_GEN_<OLD_VALUE_OF_NAME_ATTRIBUTE>"
      }
    }
  }

I took a look through the code and I think I understand what's going on.

Propose a fix:

@brendanclement brendanclement changed the title Transaction failing when updating entity with unique item Transaction failing when updating entity with unique item and not updating unique value May 26, 2023
@sahil8453
Copy link

Any update on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants