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

Commit 8b28cbb3 by Wong Yong Jie Committed by Jan Aagaard Meier

Ignore model-level validation when creating through table using string (#5556)

* Ignore model-level validation when creating join table for n:m associations.

When declaring a join table with the through option declared as a
string, Sequelize creates the through model at runtime, and copies the
model validation rules from the source table into the through model. More
often than not, the model validation rules from the source table would be
inappropriate/undesirable for the join table (since their data fields are
different anyway).

It's also worth noting that if a through model is given, this behavior
is not exhibited.

* Added unit test for issue #5556.
1 parent 2cf7f4d7
...@@ -161,7 +161,8 @@ var BelongsToMany = function(source, target, options) { ...@@ -161,7 +161,8 @@ var BelongsToMany = function(source, target, options) {
this.through.model = this.sequelize.define(this.through.model, {}, _.extend(this.options, { this.through.model = this.sequelize.define(this.through.model, {}, _.extend(this.options, {
tableName: this.through.model, tableName: this.through.model,
indexes: {}, //we dont want indexes here (as referenced in #2416) indexes: {}, //we dont want indexes here (as referenced in #2416)
paranoid: false // A paranoid join table does not make sense paranoid: false, // A paranoid join table does not make sense
validate: {} // Don't propagate model-level validations
})); }));
} else { } else {
this.through.model = this.sequelize.model(this.through.model); this.through.model = this.sequelize.model(this.through.model);
......
...@@ -40,6 +40,24 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() { ...@@ -40,6 +40,24 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
expect(AB.options.scopes).to.have.length(0); expect(AB.options.scopes).to.have.length(0);
}); });
it('should not inherit validations from parent to join table', function () {
var A = current.define('a')
, B = current.define('b', {}, {
validate: {
validateModel: function () {
return true;
}
}
})
, AB;
B.belongsToMany(A, { through: 'AB' });
AB = current.model('AB');
expect(AB.options.validate).to.deep.equal({});
});
describe('timestamps', function () { describe('timestamps', function () {
it('follows the global timestamps true option', function () { it('follows the global timestamps true option', function () {
var User = current.define('User', {}) var User = current.define('User', {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!