Skip to content

Commit

Permalink
Merge pull request sequelize#4931 from kenjitayama/paranoidDefaultVal…
Browse files Browse the repository at this point in the history
…ue_fixInstanceDestroy

Fixed instance.destroy not working for models with "paranoid" and defaultValue of deletedAt set to non-null.
  • Loading branch information
mickhansen committed Nov 28, 2015
2 parents 43a9486 + e4030b4 commit 1cd6a8a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,12 @@ Instance.prototype.destroy = function(options) {
var where = this.where();

if (this.Model._timestampAttributes.deletedAt && options.force === false) {
var field = this.Model.rawAttributes[this.Model._timestampAttributes.deletedAt].field || this.Model._timestampAttributes.deletedAt;
var values = {};
var attribute = this.Model.rawAttributes[this.Model._timestampAttributes.deletedAt]
, field = attribute.field || this.Model._timestampAttributes.deletedAt
, values = {};

values[field] = new Date();
where[field] = null;
where[field] = attribute.hasOwnProperty('defaultValue') ? attribute.defaultValue : null;

this.setDataValue(field, values[field]);

Expand Down
7 changes: 6 additions & 1 deletion test/integration/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1483,8 +1483,13 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
expect(Number(user.deletedAt)).to.equal(epoch);
return user.destroy();
}).then(function(destroyedUser) {
expect(destroyedUser.deletedAt).to.exist;
expect(Number(destroyedUser.deletedAt)).not.to.equal(epoch);
return destroyedUser.restore();
return User.findById(destroyedUser.id, { paranoid: false });
}).then(function(fetchedDestroyedUser) {
expect(fetchedDestroyedUser.deletedAt).to.exist;
expect(Number(fetchedDestroyedUser.deletedAt)).not.to.equal(epoch);
return fetchedDestroyedUser.restore();
}).then(function(restoredUser) {
expect(Number(restoredUser.deletedAt)).to.equal(epoch);
return User.destroy({where: {
Expand Down

0 comments on commit 1cd6a8a

Please sign in to comment.