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

Creating empty set makes you unable to add items #566

Open
lucas-subli opened this issue Jul 24, 2023 · 1 comment
Open

Creating empty set makes you unable to add items #566

lucas-subli opened this issue Jul 24, 2023 · 1 comment

Comments

@lucas-subli
Copy link
Contributor

I have an entity with the following attributes configured

attributes: {
    userId: { partitionKey: true },
    sk: {
	    hidden: true,
	    sortKey: true,
	    default: 'SomeDefault',
    },
    someIds: { type: 'set', setType: 'number', required: true },
},

I initially populated the data with the following input:

{ userId: 'someId', leagueIds: [] }

The dynamo DB entry was created as follows

{
  "pk": {
    "S": "someId"
  },
  "sk": {
    "S": "SomeDefault"
  },
  "leagueIds": {
    "NULL": true
  },
  "_createdAt": {
    "S": "2023-07-24T22:49:17.173Z"
  },
  "_et": {
    "S": "SomeEntity"
  },
  "_updatedAt": {
    "S": "2023-07-24T22:49:17.173Z"
  }
}

Please note the someIds field. It will be important later
I then tried to add an item to someIds using:

entity.update(
	{
		userId: 'someId',
		someIds: {
			$add: [1],
		},
	},
	{ returnValues: 'ALL_NEW' },
);

and got the following error:

ValidationException: An operand in the update expression has an incorrect data type

This only happens if I create the initial value of someIds empty (i.e. [])
If instead, I create the initial data as:

{ userId: 'someId', someIds: [ 0 ] }

Then the dynamo entry will be:

{
  "pk": {
    "S": "someIds"
  },
  "sk": {
    "S": "SomeDefault"
  },
  "someIds": {
    "NS": [
      "0"
    ]
  },
  "_createdAt": {
    "S": "2023-07-24T22:39:00.104Z"
  },
  "_et": {
    "S": "SomeEntity"
  },
  "_updatedAt": {
    "S": "2023-07-24T22:42:36.338Z"
  }
}

And I can properly add new items.

If I remove all the items, the dynamo entry will become

{
  "pk": {
    "S": "someIds"
  },
  "sk": {
    "S": "SomeDefault"
  },
  "_createdAt": {
    "S": "2023-07-24T22:39:00.104Z"
  },
  "_et": {
    "S": "SomeEntity"
  },
  "_updatedAt": {
    "S": "2023-07-24T22:42:36.338Z"
  }
}

Notice the lack of someIds field
In that situation, I am also able to properly add new items.
Thus I suspect that having someIds as null is causing the issue.

Additional info:

It may be relevant. I have this option set

removeNullAttributes: false,
@rfranco
Copy link

rfranco commented Sep 13, 2023

DynamoDB does not support empty sets

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html

Sets
DynamoDB supports types that represent sets of number, string, or binary values. All the elements within a set must be of the same type. For example, a Number Set can only contain numbers and a String Set can only contain strings.

There is no limit on the number of values in a set, as long as the item containing the values fits within the DynamoDB item size limit (400 KB).

Each value within a set must be unique. The order of the values within a set is not preserved. Therefore, your applications must not rely on any particular order of elements within the set. DynamoDB does not support empty sets, however, empty string and binary values are allowed within a set.

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