Skip to content

Commit d87e3bf

Browse files
schempybajtos
authored andcommitted
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.
1 parent 4544401 commit d87e3bf

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/datasource.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
460460
Object.keys(relations).forEach(function(rn) {
461461
var r = relations[rn];
462462
assert(DataSource.relationTypes.indexOf(r.type) !== -1, 'Invalid relation type: ' + r.type);
463+
assert(isValidRelationName(rn), 'Invalid relation name: ' + rn);
464+
463465
var targetModel, polymorphicName;
464466

465467
if (r.polymorphic && r.type !== 'belongsTo' && !r.model) {
@@ -502,6 +504,12 @@ DataSource.prototype.defineRelations = function(modelClass, relations) {
502504
}
503505
};
504506

507+
function isValidRelationName(relationName) {
508+
var invalidRelationNames = ['trigger'];
509+
510+
return invalidRelationNames.indexOf(relationName) === -1;
511+
}
512+
505513
/*!
506514
* Set up the data access functions from the data source
507515
* @param {Model} modelClass The model class

test/relations.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5483,4 +5483,24 @@ describe('relations', function() {
54835483
.catch(done);
54845484
});
54855485
});
5486+
5487+
describe('relation names', function() {
5488+
it('throws error when a relation name is `trigger`', function() {
5489+
Chapter = db.define('Chapter', {name: String});
5490+
5491+
(function() {
5492+
db.define(
5493+
'Book',
5494+
{name: String},
5495+
{
5496+
relations: {
5497+
trigger: {
5498+
model: 'Chapter',
5499+
type: 'hasMany',
5500+
},
5501+
},
5502+
});
5503+
}).should.throw('Invalid relation name: trigger');
5504+
});
5505+
});
54865506
});

0 commit comments

Comments
 (0)