From cd94be2fb8ef6918b5afffe0884fceb9d98b8754 Mon Sep 17 00:00:00 2001 From: Brian Schemp Date: Sat, 10 Dec 2016 17:49:47 -1000 Subject: [PATCH] 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. --- lib/datasource.js | 8 ++++++++ test/relations.test.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/datasource.js b/lib/datasource.js index 1b6e008a..7e4e9729 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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 diff --git a/test/relations.test.js b/test/relations.test.js index 5ef7fdd1..9beae9eb 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -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'); + }); + }); });