[英]Knex: Create migration with FOREIGN KEY
我尝试了链接中的代码来创建 FK:
我在网上遇到了一个错误:
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...
这可能来得有点晚,但错误是因为:
table.bigInteger('AddressId')
.unsigned()
.index()
.inTable('Address')
.references('id');
应该写成:
table.bigInteger('AddressId')
.unsigned()
.index()
.references('id')
.inTable('Address');
inTable
函数仅在调用引用后才存在,如文档http://knexjs.org/#Schema-inTable 中所述
调用column.references后设置外键列所在的“表”。
我认为如何分配外键已经发生了变化。
而不是这样做:
table.bigInteger('AddressId')
.unsigned()
.index()
.inTable('Address')
.references('id');
我这样做了:
table.integer('AddressId').unsigned()
tables.foreign('AddressId').references('Address.id');
它对我来说效果很好。
如需进一步参考,您可以查看此 github 要点: https://github.com/knex/knex/issues/245
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.