不要怂,就是干,撸起袖子干!

Commit 1cd6a8a1 by Mick Hansen

Merge pull request #4931 from kenjitayama/paranoidDefaultValue_fixInstanceDestroy

Fixed instance.destroy not working for models with "paranoid" and defaultValue of deletedAt set to non-null.
2 parents 43a94868 e4030b48
...@@ -830,11 +830,12 @@ Instance.prototype.destroy = function(options) { ...@@ -830,11 +830,12 @@ Instance.prototype.destroy = function(options) {
var where = this.where(); var where = this.where();
if (this.Model._timestampAttributes.deletedAt && options.force === false) { if (this.Model._timestampAttributes.deletedAt && options.force === false) {
var field = this.Model.rawAttributes[this.Model._timestampAttributes.deletedAt].field || this.Model._timestampAttributes.deletedAt; var attribute = this.Model.rawAttributes[this.Model._timestampAttributes.deletedAt]
var values = {}; , field = attribute.field || this.Model._timestampAttributes.deletedAt
, values = {};
values[field] = new Date(); values[field] = new Date();
where[field] = null; where[field] = attribute.hasOwnProperty('defaultValue') ? attribute.defaultValue : null;
this.setDataValue(field, values[field]); this.setDataValue(field, values[field]);
......
...@@ -1483,8 +1483,13 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -1483,8 +1483,13 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
expect(Number(user.deletedAt)).to.equal(epoch); expect(Number(user.deletedAt)).to.equal(epoch);
return user.destroy(); return user.destroy();
}).then(function(destroyedUser) { }).then(function(destroyedUser) {
expect(destroyedUser.deletedAt).to.exist;
expect(Number(destroyedUser.deletedAt)).not.to.equal(epoch); 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) { }).then(function(restoredUser) {
expect(Number(restoredUser.deletedAt)).to.equal(epoch); expect(Number(restoredUser.deletedAt)).to.equal(epoch);
return User.destroy({where: { return User.destroy({where: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!