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

Commit 0f786c88 by Mick Hansen

1:1 setAssociation should not clobber fields - Adds a returning option to save

1 parent 86ba3b36
......@@ -86,7 +86,11 @@ module.exports = (function() {
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
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.
return this.save(options)
......
......@@ -95,7 +95,8 @@ module.exports = (function() {
.save(
Utils._.extend({}, options, {
fields: [self.identifier],
allowNull: [self.identifier]
allowNull: [self.identifier],
association: true
})
)
.success(function() {
......
......@@ -270,6 +270,14 @@ module.exports = (function() {
options.fields = this.attributes
}
if (options.returning === undefined) {
if (options.association) {
options.returning = false
} else {
options.returning = true
}
}
var self = this
, values = {}
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
......
......@@ -179,7 +179,7 @@ module.exports = (function() {
, values = []
query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if (this._dialect.supports['RETURNING']) {
if (this._dialect.supports['RETURNING'] && (options.returning || options.returning === undefined)) {
query += " RETURNING *"
}
......
......@@ -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() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!