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

Commit f38d0cae by Jan Aagaard Meier

Run validations on the through model during add, set and create for belongsToMany. Closes #3569

1 parent 23453399
......@@ -3,6 +3,8 @@
- [BUG] Fix wrong count for `findAndCountAll` with required includes [#4016](https://github.com/sequelize/sequelize/pull/4016)
- [BUG] Fix problems related to parsing of unique constraint errors [#4017](https://github.com/sequelize/sequelize/issues/4017) and [#4012](https://github.com/sequelize/sequelize/issues/4012)
- [BUG] Fix postgres path variable being surrounded by quotes to often in unique constraint errors [#4034](https://github.com/sequelize/sequelize/pull/4034)
- [BUG] Fix `removeAttributes(id)` not setting `this.primaryKeys` to null
- [BUG] Run validations on the through model during add, set and create for `belongsToMany`
# 3.3.2
- [FIXED] upsert no longer updates with default values each time [#3994](https://github.com/sequelize/sequelize/pull/3994)
......
......@@ -457,7 +457,7 @@ BelongsToMany.prototype.injectSetter = function(obj) {
return attributes;
}.bind(this));
promises.push(association.through.model.bulkCreate(bulk, options));
promises.push(association.through.model.bulkCreate(bulk, _.defaults({ validate: true }, options)));
}
return Utils.Promise.all(promises);
......@@ -525,7 +525,7 @@ BelongsToMany.prototype.injectSetter = function(obj) {
return attributes;
}.bind(this));
promises.push(association.through.model.bulkCreate(bulk, options));
promises.push(association.through.model.bulkCreate(bulk, _.defaults({ validate: true }, options)));
}
changedAssociations.forEach(function(assoc) {
......
......@@ -860,6 +860,56 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
});
});
describe('through model validations', function () {
beforeEach(function () {
var Project = this.sequelize.define('Project', {
name: Sequelize.STRING
});
var Employee = this.sequelize.define('Employee', {
name: Sequelize.STRING
});
var Participation = this.sequelize.define('Participation', {
role: {
type: Sequelize.STRING,
allowNull: false,
validate: {
len: {
args: [2, 50],
msg: 'too bad'
}
}
}
});
Project.belongsToMany(Employee, { as: 'Participants', through: Participation });
Employee.belongsToMany(Project, { as: 'Participations', through: Participation });
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return Promise.all([
Project.create({ name: 'project 1' }),
Employee.create({ name: 'employee 1' })
]).bind(this).spread(function (project, employee) {
this.project = project;
this.employee = employee;
});
});
});
it('runs on add', function () {
return expect(this.project.addParticipant(this.employee, { role: ''})).to.be.rejected;
});
it('runs on set', function () {
return expect(this.project.setParticipants([this.employee], { role: ''})).to.be.rejected;
});
it('runs on create', function () {
return expect(this.project.createParticipant({ name: 'employee 2'}, { role: ''})).to.be.rejected;
});
});
describe('optimizations using bulk create, destroy and update', function() {
beforeEach(function() {
this.User = this.sequelize.define('User', { username: DataTypes.STRING }, {timestamps: false});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!