How do I handle broken belongTo relationships?


Due to a bug in our API server, Ember Data is creating records that doesn’t exist. It’s a bit difficult to explain, so I’ll provide an example. Let’s say we have these models.


export default DS.Model.extend({
  bar: DS.belongsTo(‘bar’, { async: false });


export default DS.Model.extend({
  someAttr: DS.attr()

When we do a on foo and bar, the API would (occassionally) return something like this:

This causes Ember Data to create a foo record with ID of “foo2”, which has all of its attributes being undefined. Is there a way to let Ember Data know that “foo2” does not exist, and prevent the creation of a phantom record?



Yes, this is possible by overriding normalizeResponse (or more accurately normalizeFindAllResponse) in your serializer. If it’s not specific to neither foo nor bar, you can do this in the application serializer.

You would basically check for multiple records in the payload and discard accordingly.