-
Notifications
You must be signed in to change notification settings - Fork 376
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
hasOne delAccessor not working #226
Comments
I'm going to take a closer look. |
I actually see 2 |
In my test file, there are 2 calls because somehow the Model is making an UPDATE query to set status_id to NULL and then making a new UPDATE query to set status_id to the previous one.. |
var _s = require('underscore.string');
var _ = require('underscore');
var ImageResource = require('./imageresource');
var Status = require('./status');
var async = require('async');
var Person = null;
module.exports = {
init: function (db, cb) {
Person = db.define("person", {
title: String,
firstname: String,
lastname: String,
job: String,
phone: String,
email: String,
sort: Number,
created_at: {type: "date", time: true},
edited_at: {type: "date", time: true}
});
Person.hasOne('imageresource', ImageResource.model(), {
autoFetch: true
}).hasOne('status', Status.model(), {
autoFetch: true
});
cb();
},
create: function (data, cb) {
Person.create({
title: data.title,
firstname: data.firstname,
lastname: data.lastname,
job: data.job,
phone: data.phone,
email: data.email,
sort: 999,
status_id: 1,
created_at: new Date()
}, cb)
},
setImageResources: function (id, resource, cb) {
Person.get(id, function (err, person) {
if (err) return cb(err);
if (resource && resource.length) {
person.setImageresource(_.first(resource), cb);
} else {
// TODO check github issues for error fix
person.removeImageresource(cb);
}
});
},
setStatus: function (id, status, cb) {
Person.get(id, function (err, person) {
if (err) return cb(err);
person.setStatus(status, cb);
})
},
addImageResources: this.setImageResources,
get: function (id, cb) {
Person.get(id, cb);
},
find: function (options, cb) {
Person.find(options, cb);
},
update: function (id, data, cb) {
Person.get(id, function (err, obj) {
if (err) return cb(err);
delete data['id'];
delete data['created_at'];
data.edited_at = new Date();
obj.save(data, cb);
});
},
remove: function (id, cb) {
Person.get(id, function (err, obj) {
if (err) return cb(err);
obj.remove(cb);
});
},
model: function () {
return Person;
}
};
|
yes, i am 100% sure its 3 calls... in this file it is actually the imageresource i want to remove... but it also works with status |
I think the hooks are being called 3 times because of the 2 associations. I only have one so it's called 2 times. I'm going to test with more associations. |
This was nasty to debug, simple to fix :) In the meantime, if you want to use the git version, do:
|
i debugged myself for 2 hours before i gave up and left it to you ;-) |
i define a status for a person:
i can set and change that status, but i cannot remove it:
i add beforeSave and afterSave hooks to the Person Model:
it seems like the hooks are called 3 times (???)
beforeSave the first time tells me the expected value (this.status_id = null)
but before afterSave is called, beforeSave is called another 2 times having the original value of status_id... after those 3 beforeSave calls, the 3 afterSave calls are called, each of them having the original value of status_id in it, and not my expected value null
help?
The text was updated successfully, but these errors were encountered: