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

After a failed .findRecord, it is impossible to create a record with the same id #219

Open
arthur5005 opened this issue May 4, 2017 · 2 comments

Comments

@arthur5005
Copy link

arthur5005 commented May 4, 2017

Hey, I ran into an issue with ember-data-localstorage today after upgrading to Ember-Data 2.13

The pattern I use with local-storage is usually:

this.store.findRecord('local-model', 'some-id')
  .catch((error) => { 
     ...
        .. if (couldNotBeFound) {
            this.store.createRecord('local-model', { id: 'some-id' })

I get an assertion failure:

The id 'some-id' has already been used with another record for modelClass 'local-model'.

It seems that .findRecord adds the model to the store even if it's not found.

@camhux
Copy link

camhux commented May 25, 2017

I ran into this too. I believe it's related to this issue with Ember Data 2.13, not this library specifically: emberjs/data#4963

The runloop workaround in that issue thread worked for me to get a "find-or-create" working. Here's what my code looks like, inside a method whose signature returns a promise resolving to a record:

return get(this, 'store').findRecord('model-type', id)
      .catch(() => {
        // `findRecord()` created an internal model for the passed ID.
        // A failed lookup causes that internal model to begin destroying.
        // It's not guaranteed to be destroyed by the time we get here, though.
        // We have to schedule anything that might touch the dematerializing internal
        // model with this ID to happen after dematerialization in the destroy queue.
        return new RSVP.Promise((fulfill) => {
          run.schedule('destroy', () => {
            fulfill(get(this, 'store').createRecord('model-type', { /* attrs */ }));
          });
        });
      });

@ldong
Copy link

ldong commented Nov 30, 2017

How about try this,

return get(this, 'store').findRecord('model', id)
  .catch(() => {
     this.store.unloadRecord(this.store.getReference('model', id).internalModel);
  });

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

3 participants