diff --git a/lib/relations.js b/lib/relations.js index 0eddb057..3b5e62ba 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -124,7 +124,7 @@ Model.hasMany = function hasMany(anotherClass, params) { if (!params.through) { // obviously, anotherClass should have attribute called `fk` - anotherClass.schema.defineForeignKey(anotherClass.modelName, fk); + anotherClass.schema.defineForeignKey(anotherClass.modelName, fk, this.modelName); } function find(id, cb) { @@ -201,7 +201,7 @@ Model.belongsTo = function (anotherClass, params) { multiple: false }; - this.schema.defineForeignKey(this.modelName, fk); + this.schema.defineForeignKey(this.modelName, fk, anotherClass.modelName); this.prototype['__finders__'] = this.prototype['__finders__'] || {}; this.prototype['__finders__'][methodName] = function (id, cb) { diff --git a/lib/schema.js b/lib/schema.js index 96205f93..2809e173 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -461,15 +461,24 @@ Schema.prototype.tableName = function (modelName) { * @param {String} className * @param {String} key - name of key field */ -Schema.prototype.defineForeignKey = function defineForeignKey(className, key) { +Schema.prototype.defineForeignKey = function defineForeignKey(className, key, foreignClassName) { // quit if key already defined if (this.definitions[className].properties[key]) return; if (this.adapter.defineForeignKey) { - this.adapter.defineForeignKey(className, key, function (err, keyType) { + var cb = function (err, keyType) { if (err) throw err; this.definitions[className].properties[key] = {type: keyType}; - }.bind(this)); + }.bind(this); + switch (this.adapter.defineForeignKey.length) { + case 4: + this.adapter.defineForeignKey(className, key, foreignClassName, cb); + break; + default: + case 3: + this.adapter.defineForeignKey(className, key, cb); + break; + } } else { this.definitions[className].properties[key] = {type: Number}; }