vogels icon indicating copy to clipboard operation
vogels copied to clipboard

Update creates another record with new values rather than updating existing record

Open osi-bhushankumar opened this issue 9 years ago • 1 comments

var InventoryModel = DynamoDb.define(TABLES.INVENTORY, {
  hashKey: 'id',
  rangeKey: 'name',
  timestamps: true,
  updatedAt: 'updatedAt',
  schema: {
    id: DynamoDb.types.uuid(),
    name: Joi.string(),
    upperName: Joi.string(),
    commodity: Joi.string(),
    subCommodity: Joi.string(),
    uom: Joi.string(), //Unit Of Measurement
    isEnable: Joi.boolean() // Delete purpose
  },
  indexes: [{
    hashKey: 'upperName', type: 'global', name: 'UpperNameIndex'
  }]
});

While Updating 'rangeKey' attributes value, it creates another record with new values rather than updating existing record.

InventoryModel.update(updateInventory, function (error, response) {
    if (!_.isEmpty(error)) {
      return callback(error, null);
    }
    return callback(null, response.attrs);
  });

osi-bhushankumar avatar Jan 20 '16 10:01 osi-bhushankumar

DynamoDB does not support updating an items keys (both hash and range key). The only way to simulate updating primary keys is to create a new entry with the new primary keys and then delete the previous item with the previous primary keys.

We could attempt to add this functionality, but would need to expose an api for passing old / new keys and also get into transaction handling.

ryanfitz avatar Jan 21 '16 17:01 ryanfitz