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

Commit ff08e29e by Mick Hansen

fix(attribute/field): provide better field support for postgres instance destroy

1 parent e52725a8
......@@ -738,14 +738,15 @@ module.exports = (function() {
return self.Model.runHooks('beforeDestroy', self, options);
}
}).then(function() {
var identifier;
var where;
if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date();
return self.save(_.extend(_.clone(options), {hooks : false}));
} else {
identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
return self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options);
where = {};
where[self.Model.rawAttributes[self.Model.primaryKeyAttribute].field] = self.get(self.Model.primaryKeyAttribute, {raw: true});
return self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), where, _.defaults(options, {limit: null}));
}
}).tap(function(result) {
// Run after hook
......
......@@ -252,6 +252,10 @@ module.exports = (function() {
if (definition.hasOwnProperty('validate')) {
self.Instance.prototype.validators[name] = definition.validate;
}
if (definition.field === undefined) {
definition.field = name;
}
});
this._hasBooleanAttributes = !!this._booleanAttributes.length;
......
......@@ -155,6 +155,22 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
});
it('should support instance.destroy()', function () {
return this.User.create().then(function (user) {
return user.destroy();
});
});
it('should support Model.destroy()', function () {
return this.User.create().bind(this).then(function (user) {
return this.User.destroy({
where: {
id: user.get('id')
}
});
});
});
});
describe('field and attribute name is the same', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!