mardi 27 septembre 2016

Ember one-to-one not serializing foreign key for one side of relationship

I have two things in a monogamous relationship. For the sake of their anonymity, we'll refer to them as a User and a Profile

user.js

export default DS.Model.extend({
    profile: DS.belongsTo('profile', { async: false }),
    name: DS.attr('string')
});

profile.js

export default DS.Model.extend({
    user: DS.belongsTo('user', { async: true }),
    active: DS.attr('boolean')
});

Users are attached at the hip with their profiles, so the profile is always sideloaded.

{
    "user": {
        "id": 23,
        "name": "Homer Simpson",
        "profile": 42
    },
    "profiles": [
        {
            "id": 42,
            "active": true,
            "profile": 23
        }
    ]
}

Now, say a user gets into trouble with the local criminal scene and decides to change his name. And to fly under the radar, he also wants his profile to be deactivated.

let user = this.get('user'),
    profile = user.get('profile');

profile.get('user.id'); // 23

user.set('name', 'Mr. Thompson');
user.save().then(() => {
    schedule.set('active', false);
    return schedule.save();
});

The payload for request.save is fine...

{
    "user": {
        "id": 23,
        "name": "Mr. Thompson",
        "profile": 42
    }
}

But for schedule.save..?

{
    "profile": {
        "id": 42,
        "active": false,
        "user": null
    }
}

Why, oh why, is the user being set to null?

There are no custom serializers coming into play nor any changes to .save

  • Ember CLI: 1.13.10
  • Ember Data: 1.13.15



Aucun commentaire:

Enregistrer un commentaire