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

Commit 1b856565 by Mick Hansen

feat(associations): belongsTo association setter now supports {save: false}, closes #2582

1 parent aefffdb0
# Next
- [BUG] Fixed issue with subquery creating `include.where` and a paranoid main model.#2749/#2769
- UniqueConstraintErrors will now extend from ValidationError making it possible to catch both with `.catch(ValidationError)`
- [FEATURE] Adds `{save: false}` for belongsTo relationship setters. `user.setOrganization(organization, {save: false})` will then only set the foreign key value, but not trigger a save on `user`.
# 2.0.0-rc4
- [INTERNALS] Update `inflection` dependency to v1.5.3
......
......@@ -128,6 +128,8 @@ module.exports = (function() {
this.set(association.identifier, value);
if (options.save === false) return;
options = Utils._.extend({
fields: [association.identifier],
allowNull: [association.identifier],
......
......@@ -261,6 +261,34 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
});
});
});
it('should set the foreign key value without saving when using save: false', function () {
var Comment = this.sequelize.define('comment', {
text: DataTypes.STRING
});
var Post = this.sequelize.define('post', {
title: DataTypes.STRING
});
Post.hasMany(Comment, {foreignKey: 'post_id'});
Comment.belongsTo(Post, {foreignKey: 'post_id'});
return this.sequelize.sync({force: true}).then(function () {
return Promise.join(
Post.create(),
Comment.create()
).spread(function (post, comment) {
expect(comment.get('post_id')).to.be.undefined;
var setter = comment.setPost(post, {save: false});
expect(setter).to.be.undefined;
expect(comment.get('post_id')).to.equal(post.get('id'));
expect(comment.changed('post_id')).to.be.true;
});
});
});
});
describe('createAssociation', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!