Throw error when model relation name is trigger

Defining a model relation with the name "trigger" causes the model not
able to insert records. No error is thrown when a model relation with
the name "trigger" is defined. Adding a check for the model relation
name "trigger" will now throw an error.
This commit is contained in:
Brian Schemp 2016-12-10 17:49:47 -10:00 committed by Miroslav Bajtoš
parent 79ec4211ab
commit cd94be2fb8
2 changed files with 28 additions and 0 deletions

View File

@ -461,6 +461,8 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
Object.keys(relations).forEach(function(rn) {
var r = relations[rn];
assert(DataSource.relationTypes.indexOf(r.type) !== -1, 'Invalid relation type: ' + r.type);
assert(isValidRelationName(rn), 'Invalid relation name: ' + rn);
var targetModel, polymorphicName;
if (r.polymorphic && r.type !== 'belongsTo' && !r.model) {
@ -503,6 +505,12 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
}
};
function isValidRelationName(relationName) {
var invalidRelationNames = ['trigger'];
return invalidRelationNames.indexOf(relationName) === -1;
}
/*!
* Set up the data access functions from the data source
* @param {Model} modelClass The model class

View File

@ -5609,4 +5609,24 @@ describe('relations', function() {
.catch(done);
});
});
describe('relation names', function() {
it('throws error when a relation name is `trigger`', function() {
Chapter = db.define('Chapter', {name: String});
(function() {
db.define(
'Book',
{name: String},
{
relations: {
trigger: {
model: 'Chapter',
type: 'hasMany',
},
},
});
}).should.throw('Invalid relation name: trigger');
});
});
});