Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update item by primary key and other conditions?

I am trying to update item by email (HASH PK), id and verifyToken. My query looks like this:

params =
  TableName: 'users'
  Key:
    email:
      S: '[email protected]'
  AttributeUpdates:
    verified:
      Action: 'PUT'
      Value:
        BOOL: true
    verifyToken:
      Action: 'DELETE'
  ExpressionAttributeValues:
    ':id': { S: '123' }
    ':verifyToken': { S: 'XXX' }
  ConditionExpression: 'id = :id and verifyToken = :verifyToken'

dynamodb.updateItem(params)

In other words I want to update Item where email = '[email protected]' AND id = '123' AND verifyToken = 'XXX', but I am getting following error:

Can not use both expression and non-expression parameters in the same request: 
Non-expression parameters: {AttributeUpdates} 
Expression parameters: {ConditionExpression}
like image 475
user606521 Avatar asked Aug 28 '15 17:08

user606521


People also ask

How do I update a DynamoDB item?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

Can we update primary key value in DynamoDB?

You cannot update the primary key attributes using UpdateItem. Instead, delete the item and use PutItem to create a new item with new attributes. The UpdateItem operation includes an Action parameter, which defines how to perform the update. You can put, delete, or add attribute values.

Can you update sort key value DynamoDB?

Can we update the sort key in DynamoDB? No, you can not update the sort key after the table is provisioned. However, you can create a new table and put the existing data in the newly created table, and delete the old table.

When you add an item the primary key attribute S are the only required attributes?

When you add an item, the primary key attributes are the only required attributes. Attribute values cannot be null. Empty String and Binary attribute values are allowed. Attribute values of type String and Binary must have a length greater than zero if the attribute is used as a key attribute for a table or index.


1 Answers

You are combining legacy parameters (AttributeUpdates), which are only there for backwards compatibility, with expression parameters (ConditionExpression). As the error states, you cannot do that.

You need to use an UpdateExpression in conjunction with your ConditionExpression.

It would be something like this. You may need to use expression attribute names/values in the UpdateExpression:

ConditionExpression: 'id = :id and verifyToken = :verifyToken'
UpdateExpression: 'SET verified = true, REMOVE verifyToken'

See this documentation for more information on update expressions

like image 112
mkobit Avatar answered Sep 18 '22 14:09

mkobit