Error re-adding a previously-deleted record

When trying to add back a record that was deleted from the store with destroyRecord() I get this error:

Attempted to handle event 'loadingData' on <saveditem:4139> while in state root.deleted.saved. Called with {_id: 74001, _label: Fetching saveditem' with id: 4139, _state: undefined, _result: undefined, _subscribers: }.

I’ve tried the method the docs recommend:

this.get('store').findRecord('saveditem', product.id, { backgroundReload: false }).then(item => {
    item.destroyRecord();
});

and also:

let item = this.get('store').peekRecord('saveditem', product.id);
if (item) {
    item.deleteRecord();
    item.save();
}

But it’s the same result. Any help appreciated.

I’m on 2.17

Hi midget2000x

As a side comment, it is not clear for me how is your usercase (the why do you want to add back a deletes record).

In Ember-data, if any record change in local, this must be saved in order of perform a request to the server where it is stored (destroyRecord() perform a request too).

But if by example, this call fails in the server, the way to “restore” the record in local, is with rollbackAttributes().

This usercase is like:

this.get('store')
  .findRecord('saveditem', product.id, { backgroundReload: false })
  .then((i) => {
    i.deleteRecord();
    i.save()
      .then(() => alert('Deleted!'))
      .catch((error) => {
        i.rollbackAttributes();
        alert('Ups, error: ' + error);
      })
  });

I hope this will help you.

Thanks for answering! I can think of a couple of use cases (more if I really thought about it). If you had a shopping cart and a customer deleted a line item. Then changed their mind and re-added it.

My use case is I am trying to keep a “saved item” list for logged in users. In my testing I discovered that a deleted item can’t be added back. Similar to the cart use case, a customer should be allowed to change their mind.

This restriction (if it is one) of not being able to add back a record with the same id as a previously deleted one, is not a pattern I’ve ever seen with any database I’ve worked with. Which makes me wonder if I am just doing it wrong. Or possibly, it could be a bug.

The error does not happen when the record is deleted, it happens when it’s being added back again AFTER deletion.

hey, check this thread Reusing Ids For Soft Deleted Records - #6 by anon89150419 I think it’s the same issue.

Indeed. Thanks so much!!