Permalink
Browse files

Support define fk with class name

  • Loading branch information...
1 parent 6dfda8f commit 7d3b1c4bd50536817ea5c53dde96f2d2e03a78f3 @1602 committed Apr 22, 2013
Showing with 14 additions and 5 deletions.
  1. +2 −2 lib/relations.js
  2. +12 −3 lib/schema.js
View
@@ -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) {
View
@@ -424,15 +424,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};
}

0 comments on commit 7d3b1c4

Please sign in to comment.