miragejs/ember-cli-mirage

Having multiple hasMany relationship throws unresolvable inverse error

Closed this issue ยท 8 comments

If I set up an app with a model that has multiple hasMany relationships to the same model, I get an error about the inverse relationships not being set up. Trying to add the relationships does not appears to fix the problem. Specifically, if I have following models set up in my app:

//  app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany('comment'),
  featuredComments: DS.hasMany('comment')
});

//  app/models/comment.js
export default DS.Model.extend({
  post: DS.belongsTo('post', { inverse: 'comments' })
});

Mirage throws the following error:

Mirage: Your 'post' model definition has multiple possible inverse relationships of type 'comment'.

Is there any way to resolve this that I'm not seeing?

Note: This appears to have started being a problem in v0.2.2

You should know there have been some significant improvements to the ORM in the 0-3 beta series, read this post for more: www.ember-cli-mirage.com/blog/2017/01/09/0-3-0-beta-series/. I'd encourage you to use it since you're dealing with hasMany relationships.

As for your particular problem you might have luck defining a null inverse on the relationships that don't have an inverse.

I'm using v0.3.0-beta.5 but {inverse: null} on the mirage model seems to have no effect and I get the same error that @kfoisy mentions above. Is there a workaround to this?

Hi @deepan83,
hi @kfoisy,

I'm facing the exact same problem right now! ๐Ÿ˜ฑ
Have you found a solution in the meantime?
I'm very interested in your insights ๐Ÿ˜Ž

@deepan83 @ChrizzDF the error is thrown because Mirage can't tell how to keep your relationships in sync, without you specifying which relationships is the inverse.

Can you post your schema for the models in question?

Just as a heads-up for anybody that comes across this issue. As @samselikoff pointed out if you declare these relationships inverse: null this works as expected. Example:

// mirage/models/user.js
export default Model.extend({
  friends: hasMany('user', { inverse: null }),
  enemies: hasMany('user', { inverse: null })
});

It appears this issue has been resolved :)

Can this be addressed without having to use a custom mirage model. It seems that defining inverse: null on the actual ED model won't work.

Being forced to use custom mirage models brings the problem of having to keep them in sync with the real model, which is something I'd prefer to avoid.

@hbrysiewicz Can we re-open this? I just created a new issue instead, since this is subtly different.