2015-02-05 146 views
5

我試圖代碼的鏈接創建FK:knex遷移創建外鍵

how to do knex.js migration

我上線的錯誤:

table.bigInteger('AddressId').unsigned().index().inTable('Address').references('id'); 

錯誤:

TypeError: Object # has no method 'inTable' at 
TableBuilder_MySQL._fn (/Users/lwang/knex/migrations/20150204161920_lei_maigration.js:15:56) at 
TableBuilder_MySQL.TableBuilder.toSQL (/Users/lwang/knex/node_modules/knex/lib/schema/tablebuilder.js:61:12) at 
SchemaCompiler_MySQL.createTable (/Users/lwang/knex/node_modules/knex/lib/schema/compiler.js:14:53) at 
SchemaCompiler_MySQL.SchemaCompiler.toSQL (/Users/lwang/knex/node_modules/knex/lib/schema/compiler.js:35:24) at 
SchemaBuilder_MySQL.SchemaBuilder.toSQL (/Users/lwang/knex/node_modules/knex/lib/schema/builder.js:41:35) at 
Runner_MySQL. (/Users/lwang/knex/node_modul... 

回答

15

這可能會晚一點,但錯誤是因爲

table.bigInteger('AddressId').unsigned().index().inTable('Address').references('id'); 

應該調用引用後寫

table.bigInteger('AddressId').unsigned().index().references('id').inTable('Address') 

的功能於表只存在如文檔中解釋http://knexjs.org/#Schema-inTable

Sets the "table" where the foreign key column is located after calling column.references.