From a8c1b5355c95da758389badca44186d47ad10f01 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Wed, 17 Jul 2013 07:17:52 -0400 Subject: [PATCH] a few more tweaks to get the foreign key support just right --- knex.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/knex.js b/knex.js index 7492fb15..8bb271ed 100644 --- a/knex.js +++ b/knex.js @@ -1181,10 +1181,14 @@ return this._indexCommand('index', columns, name); }, - // Specify a foreign key for the table. + // Specify a foreign key for the table, also getting any + // relevant info from the chain during column. foreign: function(column, name) { - var chainable = this._indexCommand('foreign', column, name); - return _.extend(chainable, ForeignChainable, _.pick(column, 'foreignColumn', 'foreignTable', 'commandOnDelete', 'commandOnUpdate')); + var chained, chainable = this._indexCommand('foreign', column, name); + if (_.isObject(column)) { + chained = _.pick(column, 'foreignColumn', 'foreignTable', 'commandOnDelete', 'commandOnUpdate'); + } + return _.extend(chainable, ForeignChainable, chained); }, // Create a new auto-incrementing column on the table. @@ -1317,7 +1321,7 @@ if (!_.isArray(columns)) columns = columns ? [columns] : []; if (index === null) { var table = this.table.replace(/\.|-/g, '_'); - index = (table + '_' + _.map(columns, function(col) { return col.name; }).join('_') + '_' + type).toLowerCase(); + index = (table + '_' + _.map(columns, function(col) { return col.name || col; }).join('_') + '_' + type).toLowerCase(); } return this._addCommand(type, {index: index, columns: columns}); },