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

Commit fa19ed9d by Mick Hansen

[fix] correctly identify attributes with field as a JSON attribute on update, fixes #2790

1 parent a07a9dd8
...@@ -315,7 +315,7 @@ module.exports = (function() { ...@@ -315,7 +315,7 @@ module.exports = (function() {
} }
var value = attrValueHash[key]; var value = attrValueHash[key];
values.push(this.quoteIdentifier(key) + '=' + this.escape(value, (!!attributes && !!attributes[key] ? attributes[key] : undefined))); values.push(this.quoteIdentifier(key) + '=' + this.escape(value, (modelAttributeMap && modelAttributeMap[key] || undefined)));
} }
var replacements = { var replacements = {
......
...@@ -896,7 +896,7 @@ module.exports = (function() { ...@@ -896,7 +896,7 @@ module.exports = (function() {
}else if (field.type === DataTypes.ARRAY(DataTypes.HSTORE)){ }else if (field.type === DataTypes.ARRAY(DataTypes.HSTORE)){
return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]"; return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]";
} }
} else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON)) { } else if (Utils._.isObject(value) && field && (field.type === DataTypes.JSON || field.type === DataTypes.JSONB)) {
value = JSON.stringify(value); value = JSON.stringify(value);
} }
......
...@@ -72,37 +72,35 @@ if (dialect.match(/^postgres/)) { ...@@ -72,37 +72,35 @@ if (dialect.match(/^postgres/)) {
}); });
}); });
it('should insert json using a custom field name', function(done) { it('should insert json using a custom field name', function() {
var self = this; var self = this;
this.UserFields = this.sequelize.define('UserFields', { this.UserFields = this.sequelize.define('UserFields', {
emergency_contact: { type: DataTypes.JSON, field: 'something_else' } emergencyContact: { type: DataTypes.JSON, field: 'emergy_contact' }
}); });
this.UserFields.sync({ force: true }).then(function() { return this.UserFields.sync({ force: true }).then(function() {
self.UserFields.create({ return self.UserFields.create({
emergency_contact: { name: 'joe', phones: [1337, 42] } emergencyContact: { name: 'joe', phones: [1337, 42] }
}).then(function(user) { }).then(function(user) {
expect(user.emergency_contact.name).to.equal('joe'); expect(user.emergencyContact.name).to.equal('joe');
done();
}); });
}); });
}); });
it('should update json using a custom field name', function(done) { it('should update json using a custom field name', function() {
var self = this; var self = this;
this.UserFields = this.sequelize.define('UserFields', { this.UserFields = this.sequelize.define('UserFields', {
emergency_contact: { type: DataTypes.JSON, field: 'something_else' } emergencyContact: { type: DataTypes.JSON, field: 'emergy_contact' }
}); });
this.UserFields.sync({ force: true }).then(function() { return this.UserFields.sync({ force: true }).then(function() {
self.UserFields.create({ return self.UserFields.create({
emergency_contact: { name: 'joe', phones: [1337, 42] } emergencyContact: { name: 'joe', phones: [1337, 42] }
}).then(function(user) { }).then(function(user) {
user.emergency_contact = { name: 'larry' }; user.emergencyContact = { name: 'larry' };
return user.save(); return user.save();
}).then(function(user) { }).then(function(user) {
expect(user.emergency_contact.name).to.equal('larry'); expect(user.emergencyContact.name).to.equal('larry');
done();
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!