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

belongsTo must have corresponding hasMany, and vice versa #436

Closed
pharcosyle opened this issue Oct 24, 2012 · 11 comments
Closed

belongsTo must have corresponding hasMany, and vice versa #436

pharcosyle opened this issue Oct 24, 2012 · 11 comments

Comments

@pharcosyle
Copy link

The newest ember-data seems to mandate that every belongsTo relationship has a corresponding hasMany relationship. For example, if I have models like this:

App.User = DS.Model.extend
    name: DS.attr 'string'

App.Note = DS.Model.extend
    author: DS.belongsTo 'App.User'

... and I attempt to set the 'author' of a Note and commit(), I get an exception in OneToManyChange.sync() because it tries to update a 'hasMany' Notes relationship in the User that doesn't exist.

Is there some way to sidestep this behavior?

@wagenet
Copy link
Member

wagenet commented Oct 24, 2012

Why do you want to?

@pharcosyle
Copy link
Author

Here are my models in a bit more detail:

App.User = DS.Model.extend
        name: DS.attr 'string'
    notes: (->
            App.Note.find author: @get('id')
            App.Note.filter (data) =>
                data.get('author.id') is @get('id')
        ).property 'id'

App.Note = DS.Model.extend
    author: DS.belongsTo 'App.User'

So when I ask for a User's notes I perform a query to load them all into the store, and then return a filter that keeps the list up to date in case the user adds any. If I changed this to

App.User = DS.Model.extend
    name: DS.attr 'string'
    notes: DS.hasMany 'App.Note'

The list of the User's notes would be kept up to date as he adds new ones, but any notes that already exist in the database wouldn't be in the list.

@sihingkk
Copy link

sihingkk commented Nov 9, 2012

Hello, I also don't want to be forced to define both side of relation.

For Instance if I have Tag and three others models, which are taggable, I don't want to have to define 3 different list in Tag (tag could have for example 100 000 id's connected to it)

@pharcosyle
Copy link
Author

@wingtsun I have a fork of ember-data in which I've removed this functionality, I can share if with you if you like.

@wagenet
Copy link
Member

wagenet commented Nov 11, 2012

I think this is something @wycats and @tomdale are working on.

@wycats
Copy link
Member

wycats commented Nov 12, 2012

@wagenet confirm

@elliterate
Copy link
Contributor

We've submitted an implementation for this with #477.

@pharcosyle
Copy link
Author

This still seems to be the case as of revision 11. What's the sitch?

@pharcosyle
Copy link
Author

Follow up: as of revison 11 overriding the private _inverseRelationshipFor prevents ember-data from trying to infer any inverses.

DS._inverseRelationshipFor = ->

@sly7-7
Copy link
Contributor

sly7-7 commented Mar 3, 2013

I don't know if this has changed during the last month, but it seems to me that there were a {inverse: null} option available when defining a relationship.
Also, I see something like one_to_none relationship: https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/relationships/one_to_none_relationships_test.js So perhaps relationship without inverse just work now...

@igorT
Copy link
Member

igorT commented Apr 15, 2013

Relationships without inverses work now. I will soon be adding explicit support for inverse:null, but in most cases it should be plug&play.

@igorT igorT closed this as completed Apr 15, 2013
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