From 2749a1f7e0f82052685e2b4445d9325036a6e257 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Wed, 27 Mar 2013 04:48:26 +0400 Subject: [PATCH] Fixes in belongsTo relation definition syntax --- lib/model.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/model.js b/lib/model.js index c6590561..bac516df 100644 --- a/lib/model.js +++ b/lib/model.js @@ -512,6 +512,12 @@ AbstractClass.include = function (objects, include, cb) { } var relation = relations[relationName]; + if (!relation) { + return function() { + cb(new Error('Relation "' + relationName + '" is not defined for ' + self.modelName + ' model')); + } + } + var req = {'where': {}}; if (!keyVals[relation.keyFrom]) { @@ -957,10 +963,14 @@ AbstractClass.belongsTo = function (anotherClass, params) { params = params || {}; if ('string' === typeof anotherClass) { params.as = anotherClass; - var anotherClassName = anotherClass.toLowerCase(); - for(var name in this.schema.models) { - if (name.toLowerCase() === anotherClassName) { - anotherClass = this.schema.models[name]; + if (params.model) { + anotherClass = params.model; + } else { + var anotherClassName = anotherClass.toLowerCase(); + for(var name in this.schema.models) { + if (name.toLowerCase() === anotherClassName) { + anotherClass = this.schema.models[name]; + } } } } @@ -969,7 +979,7 @@ AbstractClass.belongsTo = function (anotherClass, params) { this.relations[params['as']] = { type: 'belongsTo', - keyFrom: params['foreignKey'], + keyFrom: fk, keyTo: 'id', modelTo: anotherClass, multiple: false