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

Duplicate Expression Attribute Names When Updating Map Attribute Keys #460

Open
anatolzak opened this issue Jan 6, 2025 · 0 comments · May be fixed by #461
Open

Duplicate Expression Attribute Names When Updating Map Attribute Keys #460

anatolzak opened this issue Jan 6, 2025 · 0 comments · May be fixed by #461

Comments

@anatolzak
Copy link

Describe the bug

When updating an item with a map attribute, if you attempt to set multiple keys that are identical after removing non-word characters (\w), Electro will generate the same expression attribute name for both keys. This occurs even though the original keys are different, leading to conflicts in the update operation.

The following

tasks.patch({ id: '1' }).data((attrs, ops) => {
  ops.set(attrs.map['hello world'], 1)
  ops.set(attrs.map['hello    world'], 1)
}).go()

will generate the following dynamo params:

{
    "UpdateExpression": "SET #map.#helloworld = :map_u0, #map.#helloworld = :map_u1, #id = :id_u0, #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0",
    "ExpressionAttributeNames": {
        "#pk": "pk",
        "#sk": "sk",
        "#map": "map",
        "#helloworld": "hello    world", // only 1 expression attribute name was generated because both keys ('hello world' and 'hello    world') are equal after removing non-word characters
        "#id": "id",
        "#__edb_e__": "__edb_e__",
        "#__edb_v__": "__edb_v__"
    },
    "ExpressionAttributeValues": {
        ":map_u0": 1,
        ":map_u1": 1,
        ":id_u0": "1",
        ":__edb_e___u0": "tasks",
        ":__edb_v___u0": "1"
    },
    "TableName": "your_table_name",
    "Key": {
        "pk": "$taskapp#id_1",
        "sk": "$tasks_1"
    },
    "ConditionExpression": "attribute_exists(#pk) AND attribute_exists(#sk)"
}

this also throws an error in dynamo because you are not allowed to update the same field multiple times in the same operation.

Error thrown by DynamoDB client: \"Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths...

ElectroDB Version
Specify the version of ElectroDB you are using
(e.g. 3.0.1)

ElectroDB Playground Link
https://electrodb.fun/?#code/PQKgBAsg9gJgpgGzARwK5wE4Es4GcA0YuccYGeqCALgUQBYCG5YA7llXWAGbZwB2MXGBDAAUKKwBbAA5QMVMAG8wAUT5V2AT0IBlTADcsAY1IBfbhiiSwAIkRwjVSzABGNgNzijUPrgVUGFwRSAF5bTShUDAB9AKC4aL4GSTgPcVAwABUGXABrITUNKk1hMW9ffxz8sDC+OBZVdS0AClEwJTb2sElYRAAuDq6u-iLNAZsAvNwbfE6h-UxcLB9xgEYZua7iDEMTccnchmlpG03TWaGGKicsF1QqPAHFTfasGCeXruLpOHG-bD4AHMNkMhuQ0FhyO8wE50J9zp9JEcPqCvpofgMAOQMPiaTHwi5dBFDLACOAAD0eg1B0ksACsHDQUaiwNJcsyWdwcAhoTY2SDOWBvDIoEsHgMANo2N42AC6n3axJZeQ5LK43N5eQFnOAwCF5CupGFsjFpFycE0Qi4clZTA0DCQuDkCnNJTQmBwuAVXWNovYvzAEvlnNMBLOnSVyjiwTAoYAlJ5RAdcAA6aRXIx0ZrKN5Y1aY2NxlMwK4MZrNK5OWhQaS4OM1AB81JrqeIVAr1wwqaR0glmLoiAQUFYch5mNlhFWcc6LZTbY7VZTPb7A4QQ6GLFHMHHk+npiLgKgzTjQA

Entity/Service Definitions
Include your entity model (or a model that sufficiently recreates your issue) to help troubleshoot.

{
  model: {
    entity: "tasks",
    version: "1",
    service: "taskapp"
  },
  attributes: {
    id: {
      type: "string",
      required: true
    },
    map: {
      type: 'any'
    },
  },
  indexes: {
    projects: {
      pk: {
        field: "pk",
        composite: ["id"]
      },
      sk: {
        field: "sk",
        composite: []
      }
    },
  }
}

Expected behavior

Separate expression attribute names should be generated for each unique key.

Errors

Error thrown by DynamoDB client: \"Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths...
@anatolzak anatolzak linked a pull request Jan 6, 2025 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant