fix recursive calls if create belongsTo model in beforeCreate hook
Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
parent
d5dcf0a966
commit
40f03f084b
|
@ -1192,11 +1192,16 @@ BelongsTo.prototype.create = function(targetModelData, cb) {
|
|||
modelTo.create(targetModelData, function(err, targetModel) {
|
||||
if(!err) {
|
||||
modelInstance[fk] = targetModel[pk];
|
||||
if (modelInstance.isNewRecord()) {
|
||||
self.resetCache(targetModel);
|
||||
cb && cb(err, targetModel);
|
||||
} else {
|
||||
modelInstance.save(function(err, inst) {
|
||||
if (cb && err) return cb && cb(err);
|
||||
self.resetCache(targetModel);
|
||||
cb && cb(err, targetModel);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
cb && cb(err);
|
||||
}
|
||||
|
|
|
@ -1657,6 +1657,23 @@ describe('relations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should allow to create belongsTo model in beforeCreate hook', function (done) {
|
||||
var mind;
|
||||
Fear.beforeCreate = function (next) {
|
||||
this.mind.create(function (err, m) {
|
||||
mind = m;
|
||||
if (err) next(err); else next();
|
||||
});
|
||||
};
|
||||
Fear.create(function (err, fear) {
|
||||
should.not.exists(err);
|
||||
should.exists(fear);
|
||||
fear.mindId.should.be.equal(mind.id);
|
||||
should.exists(fear.mind());
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('belongsTo with scope', function () {
|
||||
|
@ -1680,9 +1697,12 @@ describe('relations', function () {
|
|||
p.personId.should.equal(person.id);
|
||||
person.name.should.equal('Fred');
|
||||
person.passportNotes.should.equal('Some notes...');
|
||||
p.save(function (err, passport) {
|
||||
should.not.exists(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should find record on scope', function (done) {
|
||||
Passport.findOne(function (err, p) {
|
||||
|
|
Loading…
Reference in New Issue