Saving a model with a belongsTo reference set to null


I have a model like this:

App.Group = DS.Model.extend({
    parent: DS.belongsTo('group', {inverse: 'items'}),
    items: DS.hasMany('group', {inverse: 'parent'})

Now I have an instance of this group with a valid parent and I like to set the parent to null / undefined.

console.log(item.get('')) // prints the parents id 
item.set('parent', null)
console.log(item.get('')) // prints undefined => console.log(item.get(''))) // prints the old parents id!

In the network request the old parent id is sent, so it is clear where the old parent id comes from after the item has beed saved. But why is the item saved with the old parent?

When I change the parent to another valid parent, it works as excepected. But how can I make the item have no parent? The parent item itself should not be removed.

I don’t know if it’s relevant, but I’m using the DS.RestAdapter with Ember Simple Auth.

Ember: 3.8.2
Ember Data: 3.8.0
Ember Simple Auth: 1.8.2

Thanks for your help.

That’s surprising and seems like a bug!

Quick question - after you save the item, what data is returned from the server?


Yes it was a bug, but in my server code grummel.

In my search for the bug, I had mixed up the request and response headers. The request header had ‘parent: null’, but the response header had the old parent id.

So thank you for your answer, it was exactly the hint I needed.

1 Like