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

Commit 44851dfa by Mick Hansen

Merge pull request #1196 from mickhansen/association-attribute-clobber-fix

1:1 setAssociation should not clobber fields (fixes #1183)
2 parents 86ba3b36 506f67df
...@@ -86,7 +86,11 @@ module.exports = (function() { ...@@ -86,7 +86,11 @@ module.exports = (function() {
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null
options = Utils._.extend({ fields: [ self.identifier ], allowNull: [self.identifier] }, options) options = Utils._.extend({
fields: [ self.identifier ],
allowNull: [self.identifier],
association: true
}, options)
// passes the changed field to save, so only that field get updated. // passes the changed field to save, so only that field get updated.
return this.save(options) return this.save(options)
......
...@@ -95,7 +95,8 @@ module.exports = (function() { ...@@ -95,7 +95,8 @@ module.exports = (function() {
.save( .save(
Utils._.extend({}, options, { Utils._.extend({}, options, {
fields: [self.identifier], fields: [self.identifier],
allowNull: [self.identifier] allowNull: [self.identifier],
association: true
}) })
) )
.success(function() { .success(function() {
......
...@@ -270,6 +270,14 @@ module.exports = (function() { ...@@ -270,6 +270,14 @@ module.exports = (function() {
options.fields = this.attributes options.fields = this.attributes
} }
if (options.returning === undefined) {
if (options.association) {
options.returning = false
} else {
options.returning = true
}
}
var self = this var self = this
, values = {} , values = {}
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored) , updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
......
...@@ -173,13 +173,15 @@ module.exports = (function() { ...@@ -173,13 +173,15 @@ module.exports = (function() {
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
*/ */
updateQuery: function(tableName, attrValueHash, where, options, attributes) { updateQuery: function(tableName, attrValueHash, where, options, attributes) {
options = options || {}
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull, options) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull, options)
var query var query
, values = [] , values = []
query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>" query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if (this._dialect.supports['RETURNING']) { if (this._dialect.supports['RETURNING'] && (options.returning || options.returning === undefined)) {
query += " RETURNING *" query += " RETURNING *"
} }
......
...@@ -133,6 +133,36 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -133,6 +133,36 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
}) })
it('should not clobber atributes', function (done) {
var Comment = this.sequelize.define('comment', {
text: DataTypes.STRING
});
var Post = this.sequelize.define('post', {
title: DataTypes.STRING
});
Post.hasOne(Comment);
Comment.belongsTo(Post);
this.sequelize.sync().done(function (err) {
Post.create({
title: 'Post title',
}).done(function(err, post) {
Comment.create({
text: 'OLD VALUE',
}).done(function(err, comment) {
comment.setPost(post).done(function(err) {
expect(comment.text).to.equal('UPDATED VALUE');
done()
});
comment.text = 'UPDATED VALUE';
});
});
})
})
}) })
describe("Foreign key constraints", function() { describe("Foreign key constraints", function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!