【问题标题】:Knex: Create migration with FOREIGN KEYKnex:使用 FOREIGN KEY 创建迁移
【发布时间】:2015-02-05 17:58:20
【问题描述】:

我尝试了链接中的代码来创建FK:

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...

【问题讨论】:

    标签: javascript sql knex.js


    【解决方案1】:

    这可能来得有点晚,但错误是因为:

    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。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2019-12-18
      • 2018-09-16
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多