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:
parent
79ec4211ab
commit
cd94be2fb8
|
@ -461,6 +461,8 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
|
||||||
Object.keys(relations).forEach(function(rn) {
|
Object.keys(relations).forEach(function(rn) {
|
||||||
var r = relations[rn];
|
var r = relations[rn];
|
||||||
assert(DataSource.relationTypes.indexOf(r.type) !== -1, 'Invalid relation type: ' + r.type);
|
assert(DataSource.relationTypes.indexOf(r.type) !== -1, 'Invalid relation type: ' + r.type);
|
||||||
|
assert(isValidRelationName(rn), 'Invalid relation name: ' + rn);
|
||||||
|
|
||||||
var targetModel, polymorphicName;
|
var targetModel, polymorphicName;
|
||||||
|
|
||||||
if (r.polymorphic && r.type !== 'belongsTo' && !r.model) {
|
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
|
* Set up the data access functions from the data source
|
||||||
* @param {Model} modelClass The model class
|
* @param {Model} modelClass The model class
|
||||||
|
|
|
@ -5609,4 +5609,24 @@ describe('relations', function() {
|
||||||
.catch(done);
|
.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');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue