Merge pull request #423 from clarkorz/hasOne-findOrCreate
use findOrCreate in hasOne#create
This commit is contained in:
commit
cc1791b01d
|
@ -1539,22 +1539,17 @@ HasOne.prototype.create = function (targetModelData, cb) {
|
|||
this.definition.applyScope(modelInstance, query);
|
||||
this.definition.applyProperties(modelInstance, targetModelData);
|
||||
|
||||
modelTo.findOne(query, function(err, result) {
|
||||
if(err) {
|
||||
cb(err);
|
||||
} else if(result) {
|
||||
cb(new Error('HasOne relation cannot create more than one instance of '
|
||||
+ modelTo.modelName));
|
||||
} else {
|
||||
modelTo.create(targetModelData, function (err, targetModel) {
|
||||
if (!err) {
|
||||
modelTo.findOrCreate(query, targetModelData, function (err, targetModel, created) {
|
||||
if (err) {
|
||||
return cb && cb(err);
|
||||
}
|
||||
if (created) {
|
||||
// Refresh the cache
|
||||
self.resetCache(targetModel);
|
||||
cb && cb(err, targetModel);
|
||||
} else {
|
||||
cb && cb(err);
|
||||
}
|
||||
});
|
||||
cb && cb(new Error('HasOne relation cannot create more than one instance of '
|
||||
+ modelTo.modelName));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue