Reputation: 18682
I want to define multiple hasMany relationships on Ember Data model of parent type, but I don't have a clue how to specify inverses for this:
Profile = DS.Model.extend
# ...
friends: DS.hasMany 'profile', async: true
observed: DS.hasMany 'profile', async: true
observers: DS.hasMany 'profile', async: true
It would be easy for me to handle such properties in database, however Ember Data doesn't support this. Is this possible to have this 3 hasMany relationships defined without creating another models (eg. Friend, Observer).
Error I'm getting:
Error: Assertion Failed: You defined the 'friends' relationship on app@model:profile:, but multiple possible inverse relationships of type app@model:profile: were found on app@model:profile:. Look at http://emberjs.com/guides/models/defining-models/#toc_explicit-inverses for how to explicitly specify inverses
Upvotes: 1
Views: 1312
Reputation: 6366
Ember Data wants to have a foreign key on the belongsTo side so that it can wire up the models in either direction for you. Ember Data does not directly support many-many relationships hence needing a second join model.
This is pretty much in line with how you model data in a SQL database.
One of the things I dislike most with Ember Data is the fact it wants ids on the hasMany side as well as the belongsTo side. This is not a great approach when you have high cardinality relationships as it means large payloads and inefficient queries when a simple foreign key based lookup would be more efficient. It also gives the illusion that you could perhaps avoid a second model and your Profile model could just have arrays of ids for each relationship but it just doesn't work that way currently.
Upvotes: 1
Reputation: 18682
This works for now:
Profile = DS.Model.extend
# ...
friends: DS.hasMany 'profile', async: true
observed: DS.hasMany 'observer', async: true, inverse: 'observer'
observers: DS.hasMany 'observer', async: true, inverse: 'observed'
Observer = DS.Model.extend
observer: DS.belongsTo 'profile', async: true
observed: DS.belongsTo 'profile', async: true
However, I'm still interested if there is a way to do it without defining another model.
Upvotes: 0