From e5824356ca1319f64698ddaaaec993cb3f2d9cb5 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 4 Dec 2013 21:38:40 -0800 Subject: [PATCH] Fix belongsTo relation --- lib/datasource.js | 26 ++++++++++++-------------- lib/relations.js | 2 +- package.json | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index 62e1a688..0f0755a2 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -232,7 +232,7 @@ DataSource.prototype.setup = function(name, settings) { } connector = require(name); } catch (e) { - return console.log('\nWARNING: LoopbackData connector "' + name + '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + name, '\n'); + return console.log('\nWARNING: LoopBack connector "' + name + '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + name, '\n'); } } } @@ -920,7 +920,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) { } var self = this; - var schemaName = this.name || this.connector.name; + var schemaName = this.connector.name || this.name; var tasks = [ this.discoverModelProperties.bind(this, modelName, options), @@ -1027,14 +1027,13 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) { console.log('Foreign keys: ', fks); } + schema.options.relations = {}; foreignKeys.forEach(function (fk) { var propName = fromDBName(fk.pkTableName, true); - schema.properties[propName] = { - type: fromDBName(fk.pkTableName, false), - relation: { - type: 'belongsTo', - foreignKey: fromDBName(fk.pkColumnName, true) - } + schema.options.relations[propName] = { + model: fromDBName(fk.pkTableName, false), + type: 'belongsTo', + foreignKey: fromDBName(fk.fkColumnName, true) }; var key = fk.pkOwner + '.' + fk.pkTableName; @@ -1175,14 +1174,13 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) { console.log('Foreign keys: ', fks); } + schema.options.relations = {}; foreignKeys.forEach(function (fk) { var propName = fromDBName(fk.pkTableName, true); - schema.properties[propName] = { - type: fromDBName(fk.pkTableName, false), - relation: { - type: 'belongsTo', - foreignKey: fromDBName(fk.pkColumnName, true) - } + schema.options.relations[propName] = { + model: fromDBName(fk.pkTableName, false), + type: 'belongsTo', + foreignKey: fromDBName(fk.fkColumnName, true) }; var key = fk.pkOwner + '.' + fk.pkTableName; diff --git a/lib/relations.js b/lib/relations.js index 7cf2aaa9..a5b043f4 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -195,7 +195,7 @@ Relation.belongsTo = function (anotherClass, params) { } } - var idName = this.dataSource.idName(this.modelName) || 'id'; + var idName = this.dataSource.idName(anotherClass.modelName) || 'id'; var methodName = params.as || i8n.camelize(anotherClass.modelName, true); var fk = params.foreignKey || methodName + 'Id'; diff --git a/package.json b/package.json index 77ee23d5..34712bfc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-datasource-juggler", - "version": "1.2.5", + "version": "1.2.6", "description": "LoopBack DataSoure Juggler", "keywords": [ "StrongLoop",