diff --git a/lib/relation-definition.js b/lib/relation-definition.js index f913cc6b..4674173b 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1431,8 +1431,14 @@ HasOne.prototype.update = function (targetModelData, cb) { }; HasOne.prototype.destroy = function (cb) { - - + this.fetch(function(err, inst) { + if (inst instanceof ModelBaseClass) { + inst.destroy(cb); + } else { + cb(new Error('HasOne relation ' + definition.name + + ' is empty')); + } + }); }; /** diff --git a/test/relations.test.js b/test/relations.test.js index b517e236..bba27a0e 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1269,6 +1269,29 @@ describe('relations', function () { }); }); + it('should destroy the related item on scope', function(done) { + Supplier.findById(supplierId, function(e, supplier) { + should.not.exist(e); + should.exist(supplier); + supplier.account.destroy(function(err) { + should.not.exist(e); + done(); + }); + }); + }); + + it('should get the related item on scope - verify', function(done) { + Supplier.findById(supplierId, function(e, supplier) { + should.not.exist(e); + should.exist(supplier); + supplier.account(function(err, act) { + should.not.exist(e); + should.not.exist(act); + done(); + }); + }); + }); + }); describe('hasAndBelongsToMany', function () {