v0.9.8 - 'Unlink' never removes previous link relation changes.
See original GitHub issueLove the package! It’s been very helpful of late, but I’ve noticed an issue 😃
I noticed that if you persist a parent model in memory, the relationChanges array just continues to grow as you link and unlink objects to the parent object.
It looks like there is a check failing in the unlink function for a couple of reasons.
In source, the function looks like this:
exports.unlink = function unlink(obj, options, callback) {
if (typeof(options) === 'string') {
options = {name: options};
}
var opts = h.$extend({
name: 'default'
}, options);
callback = h.getCallback(arguments);
var changes = this.relationChanges;
for (var i in changes) {
if (changes.hasOwnProperty(i) &&
changes[i].name === opts.name &&
h.checkEqual(changes[i].object, obj)) {
delete this.relationChanges[i];
}
}
this.relationChanges.push({
action: 'unlink',
options: opts,
callback: callback,
object: obj
});
};
First, changes[i].name === opts.name will always evaluate false, as the relation name is actually at changes[i].options.name.
Second, delete this.relationchanges will leave an ‘undefined’ object in the array at the index, causing issues further downstream. You’re better off using this.relationChanges.splice(i, 1);
Third, in the h.checkEqual() function, there is a conditional as below:
else if (obj1.hasOwnProperty('modelName') && obj2.hasOwnProperty('modelName') &&
obj1.modelName === obj2.modelName) {
This will exclude the entire second half of the function, as the modelName attribute is located at obj1.__proto__.modelName
I’ll prepare a PR with the appropriate changes to fix the issue. I’m not fussed if you would rather approach the issue in a different manner, but we’re keen to get this merged to avoid issues with patching at build.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)

Top Related StackOverflow Question
I’ve put the changes here:
https://github.com/lp-belliot/nohm/tree/feature/fix_bug_in_unlink_function
Not sure what to do given master is well past the v0.9.8 commit.
Sure, I just won’t spend much time on it in v1. 😉
But I can certainly read, accept/decline PRs and publish new versions.
The checkEqual is still the same in v2, the other 2 problems are already fixed in v2.
@lp-belliot thanks for the report & PR! Can you add some unit tests that fail without your changes? If possible one for each of the 3 problems you found.