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

Commit 3b656127 by Mick Hansen

Merge pull request #5219 from rmzg/master

Fixes a bug with user defined attribute names overwriting builtins.
2 parents 4651cfbe 41fc3d21
......@@ -196,7 +196,9 @@ Instance.prototype.get = function(key, options) { // testhint options:none
return this.dataValues[key];
}
}
return this.dataValues[key];
if (this.dataValues) {
return this.dataValues[key];
}
}
if (this._hasCustomGetters || (options && options.plain && this.$options.include) || (options && options.clone)) {
......
......@@ -74,10 +74,11 @@ var Model = function(name, attributes, options) {
}
});
this.attributes = this.rawAttributes = Utils._.mapValues(attributes, function(attribute, name) {
if (!Utils._.isPlainObject(attribute)) {
attribute = { type: attribute };
}
this.attributes = this.rawAttributes = Utils._.mapValues(attributes, function(attribute, name) {
if (!Utils._.isPlainObject(attribute)) {
attribute = { type: attribute };
}
attribute = this.sequelize.normalizeAttribute(attribute);
......@@ -923,7 +924,14 @@ Model.prototype.refreshAttributes = function() {
this.Instance.prototype._hasCustomGetters = Object.keys(this.Instance.prototype._customGetters).length;
this.Instance.prototype._hasCustomSetters = Object.keys(this.Instance.prototype._customSetters).length;
Object.defineProperties(this.Instance.prototype, attributeManipulation);
Object.keys(attributeManipulation).forEach((function(key){
if (this.Instance.prototype[key] !== undefined) {
this.sequelize.log("Not overriding built-in method from model attribute: " + key);
return;
}
Object.defineProperty(this.Instance.prototype, key, attributeManipulation[key]);
}).bind(this));
this.Instance.prototype.rawAttributes = this.rawAttributes;
this.Instance.prototype.attributes = Object.keys(this.Instance.prototype.rawAttributes);
......
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../../support')
, DataTypes = require(__dirname + '/../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('not breaking built-ins', function() {
it('it should not break instance.set by defining a model set attribute', function() {
var User = this.sequelize.define('OverWrittenKeys', {
set:DataTypes.STRING
});
var user = User.build({set: 'A'});
expect(user.get('set')).to.equal('A');
user.set('set', 'B');
expect(user.get('set')).to.equal('B');
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!