Data: Invalid relationship inverse configuration not asserted (multiple inverses)

Created on 13 Apr 2018  路  3Comments  路  Source: emberjs/data

Don't know how to reproduce yet outside a complex app...

ED 2.18.2

Triggered by fetchLink

vendor-3bd14d61504f3d17c7c371655fee128d.js:53572 Uncaught TypeError: Cannot read property 'get' of undefined
    at BelongsToRelationship.addInternalModel (vendor-3bd14d61504f3d17c7c371655fee128d.js:142002)
    at BelongsToRelationship.addInternalModel (vendor-3bd14d61504f3d17c7c371655fee128d.js:143153)
    at vendor-3bd14d61504f3d17c7c371655fee128d.js:143209
    at tryCatcher (vendor-3bd14d61504f3d17c7c371655fee128d.js:65295)
    at invokeCallback (vendor-3bd14d61504f3d17c7c371655fee128d.js:65477)
    at publish (vendor-3bd14d61504f3d17c7c371655fee128d.js:65462)
    at Queue.invoke (vendor-3bd14d61504f3d17c7c371655fee128d.js:27963)
    at Queue.flush (vendor-3bd14d61504f3d17c7c371655fee128d.js:27848)
    at DeferredActionQueues.flush (vendor-3bd14d61504f3d17c7c371655fee128d.js:28035)
    at Backburner.end (vendor-3bd14d61504f3d17c7c371655fee128d.js:28169)
  Relationship.prototype.addInternalModel = function addInternalModel(internalModel, idx) {
    if (!this.members.has(internalModel)) {
      this.members.addWithIndex(internalModel, idx);
      this.notifyRecordRelationshipAdded(internalModel, idx);
      if (this.inverseKey) {

         // -----  internalModel._relationships is undefined here!

         internalModel._relationships.get(this.inverseKey).addInternalModel(this.internalModel);
      } else {
        if (!internalModel._implicitRelationships[this.inverseKeyForImplicit]) {
          internalModel._implicitRelationships[this.inverseKeyForImplicit] = new Relationship(this.store, internalModel, this.key, { options: { async: this.isAsync }, type: this.parentType });
        }
        internalModel._implicitRelationships[this.inverseKeyForImplicit].addInternalModel(this.internalModel);
      }
      this.internalModel.updateRecordArrays();
    }
    this.setHasData(true);
  };
Good for New Contributors Improvement test-contribution

Most helpful comment

This is what I had: Assume a 1:1 relationship between pet 1---1 owner and pet 1---1 vet. Yes, bad example, but can't think of a better one.

model:pet
  owner: belongsTo('person'),
  vet: belongsTo('person')

model:person
  pet: belongsTo('pet')

Adding a pet to an owner worked, but adding a pet to a vet failed as above.

Fix:

model:pet
  owner: belongsTo('person', {inverse:null}),
  vet: belongsTo('person', {inverse:null})

model:person
  pet: belongsTo('pet', {inverse:null})

Interestingly this worked in older ED versions without specifying inverse: null.

All 3 comments

Turned out to be caused by the same issue as #5398

It would be nice if the Model classes could do a schema verification phase during the construction process. Would this be feasible? Or could a test be created for each model by the blueprint that would run a verification?

This is what I had: Assume a 1:1 relationship between pet 1---1 owner and pet 1---1 vet. Yes, bad example, but can't think of a better one.

model:pet
  owner: belongsTo('person'),
  vet: belongsTo('person')

model:person
  pet: belongsTo('pet')

Adding a pet to an owner worked, but adding a pet to a vet failed as above.

Fix:

model:pet
  owner: belongsTo('person', {inverse:null}),
  vet: belongsTo('person', {inverse:null})

model:person
  pet: belongsTo('pet', {inverse:null})

Interestingly this worked in older ED versions without specifying inverse: null.

@BryanCrotaz in older ED versions, if you didn't specify the inverse, whichever (owner / vet) you accessed first would get the relationship and the other would be broken (albeit silently).

You should have gotten an error here though? We do verify inverses are configured in a valid way. Let's add a test case for this setup to figure out why we didn't.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ef4 picture ef4  路  4Comments

bekicot picture bekicot  路  4Comments

NullVoxPopuli picture NullVoxPopuli  路  5Comments

stefanpenner picture stefanpenner  路  4Comments

maschwenk picture maschwenk  路  5Comments