Search code examples
python-3.xpynamodb

PynamoDb - Increment operation


Is there a way to do the below code snippet using pynamodb?

dynamoDB.updateItem({
  TableName: "Users",
  Key: { "UserId": { S: "c6af9ac6-7b61" } },
  ExpressionAttributeValues: { ":inc": {N: "1"} },
  UpdateExpression: "ADD loginCount :inc"
})

I can see it is supported, as per the test here and the doc here , but when I try to do an add operation on a numeric field as shown below, it throws AttributeError: 'int' object has no attribute 'add' exception.

test_table.balance.add(int(dep_amount))


Solution

  • The add operation is an update action, so it should be included in a list of actions passed to test_user.update as follows:

    test_user.update(actions=[User.balance.add(int(dep_amount))])
    

    The pynamodb docs include a section on Updating Items.