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

Having multiple hasMany relationship throws unresolvable inverse error #996

Closed
kfoisy opened this issue Jan 23, 2017 · 8 comments
Closed

Comments

@kfoisy
Copy link

kfoisy commented Jan 23, 2017

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

@samselikoff
Copy link
Collaborator

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.

@deepan83
Copy link

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?

@ChrizzDF
Copy link
Contributor

ChrizzDF commented Apr 28, 2017

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 😎

@samselikoff
Copy link
Collaborator

@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?

@LevelbossMike
Copy link
Contributor

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 })
});

@hbrysiewicz
Copy link
Collaborator

It appears this issue has been resolved :)

@lougreenwood
Copy link

lougreenwood commented Feb 15, 2021

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.

@lougreenwood
Copy link

#2123

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

7 participants