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

Weird behaviour when including relationships in response #4889

Closed
topaxi opened this issue Mar 25, 2017 · 2 comments · Fixed by #5410
Closed

Weird behaviour when including relationships in response #4889

topaxi opened this issue Mar 25, 2017 · 2 comments · Fixed by #5410

Comments

@topaxi
Copy link
Contributor

topaxi commented Mar 25, 2017

I'm currently trying to load quite a few records and their related records in one request. Sometimes Ember Data seems to fetch related records even though they were included in the request. Another weird thing is, that switching to a different model it seems like reloaded models change or unset their relations.

I've made a little twiddle for reproduction:
https://ember-twiddle.com/bbeb5040ebe8e342ecf854199e1bb0a6

Visiting this link, you'll see a list of Playlists, clicking one of those fetches one of those playlists with all their related data in one request (using mirage). On the first visit, it seems to make unnecessary requests to already included records of PlaylistTrack.

Switching between Playlists also seem to have some weird behavior, where Track relations to Artist seem to unset them on subsequent requests.

@bmac
Copy link
Member

bmac commented Mar 26, 2017

Ember Data seems to be getting confused by the artist records that the backend is returning.

{
  attributes: { name: "Pennywise" },
  id: "pennywise",
  relationships: {
    tracks: { 
      data: []
    }
  } 
  type: "artists"
}

They contain a tracks relationship with no records, and because they get parsed after the track records (because they are after the track records in the includes array) Ember Data think their info is more up to date and unsets the artist belongsTo on the track side of the relationship.

When I remove the tracks hasMany from mirage/models/track.js mirage stops sending the empty array and Ember Data appears to start behaving normally again.

export default Model.extend({
  //tracks: hasMany()
});

Debugging Ember Data's behavior around JSON API payloads like this is often frustrating and non obvious. Perhaps Ember Data could do a better job of validating the JSON API payload before pushing it into the store to flag contradictions like this.

@topaxi
Copy link
Contributor Author

topaxi commented Mar 29, 2017

Oh nice thanks this works! 👍
Should I close this issue and report this to ember-cli-mirage?

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

Successfully merging a pull request may close this issue.

2 participants