Skip to content

bug: async-to-async relationships may error when using unloadAll on both types #9151

@elfin-sbreuers

Description

@elfin-sbreuers

Reproduction

I am observing the following error with ember-data version 4.12.4

vendor.js:16753 Uncaught (in promise) Error: Assertion Failed: Expected a stable identifier
    at assert (vendor.js:16753:15)
    at LegacyWrapper.disconnectRecord (vendor.js:87530:73)
    at JSONAPICache.unloadRecord (vendor.js:70505:26)
    at vendor.js:87974:19
    at Store._join (vendor.js:90492:11)
    at InstanceCache.unloadRecord (vendor.js:87959:20)
    at vendor.js:88012:22
    at Map.forEach (<anonymous>)
    at InstanceCache.clear (vendor.js:88010:25)
    at vendor.js:91938:36

It happens when using unloadRecord and some of the models are only partly loaded by being referenced in the relationships of an already loaded model.

import Model from '@ember-data/model';
import { hasMany } from '@ember-data/model';

export default class FooModel extends Model {
  @hasMany('bar', { async: true, inverse: 'foos' }) bars;
}
//app/models/bar.js
import Model from '@ember-data/model';
import { hasMany } from '@ember-data/model';

export default class BarModel extends Model {
  @hasMany('foo', { async: true, inverse: 'bars' }) foos;
}
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class TestRoute extends Route {
  @service store;
  model() {
    this.store.push({
      data: [
        {
          id: 1,
          type: 'foo',
          relationships: { bars: { data: [{ id: 1, type: 'bar' }] } },
        },
        // Commenting that in would solve the issue.
        // Meaning having the related resource fully loaded.
        /*{
          id: 1,
          type: 'bar',
          relationships: { foos: { data: [{ id: 1, type: 'foo' }] } },
        },*/
      ],
    });
    ['foo', 'bar'].forEach((modelName) => this.store.unloadAll(modelName));
    return this.store.peekAll('foo');
  }
}

The problem can be solved (or worked around) if the model the relationship points to is fully loaded by using include in the request or just loading the partly loaded model pointed to by the relationship directly.

It would be nice if the related models do not need to be fully loaded first before being able to clean the store from models, that are just partly loaded.

Version

[email protected] /home/breuerss/devel/flexypage/bug
├─┬ @ember/[email protected]
│ ├─┬ @embroider/[email protected]
│ │ └── [email protected] deduped
│ └── [email protected] deduped
├─┬ [email protected]
│ └─┬ @ember-data/[email protected]
│   └─┬ [email protected]
│     └── [email protected] deduped
└── [email protected]

[email protected] /home/breuerss/devel/flexypage/bug
├─┬ [email protected]
│ └── [email protected] deduped
└── [email protected]

[email protected] /home/breuerss/devel/flexypage/bug
└── [email protected]

A fix for the 4.12 LTS would be much appreciated. If you need any help reproducing the issue I would gladly assist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🏷️ bugThis PR primarily fixes a reported issue🏷️ featThis PR introduces a new featureconcept-reviewTracks PRs that introduce new concepts that need reviewed for discussion

    Projects

    Status

    needs champion

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions