【问题标题】:Knex chaining table creation in migrations迁移中的 Knex 链表创建
【发布时间】:2021-04-30 01:02:07
【问题描述】:

我有以下迁移代码:

exports.up = function(knex) {
    return knex.schema.createTable('users', function(table){
        table.increments();
        table.string('email', 100).unique().notNullable();
        table.string('password').notNullable();
      })
      .createTable('posts', function(table){
        table.increments();
        table.string('title').notNullable();
        table.string('content').notNullable();
        table.timestamp('created_at').defaultTo(knex.fn.now());

        table.int('userId').notNullable().references('id').inTable('users');
      })
      .createTable('comments', function(table){
        table.increments();
        table.string('content').notNullable();
        table.timestamp('created_at').defaultTo(knex.fn.now());
        
        table.int('userId').notNullable().references('id').inTable('users');
        table.int('postId').notNullable().references('id').inTable('posts');
      })
};

exports.down = function(knex) {
  return knex.schema.dropTable('users')
  .dropTable('posts')
  .dropTable('comments');
};

它产生以下错误:

migration failed with error: create table "posts" ("id" serial primary key, "title" varchar(255) not null, "content" varchar(255) not null, "created_at" timestamptz default CURRENT_TIMESTAMP, "userId" undefined not null) - type "undefined" does not exist

似乎很难连接用户和帖子表,但我不确定为什么。

【问题讨论】:

  • 我照原样复制了它,它工作正常吗?不确定这是否是一回事,但您可能希望将 down 函数更改为以相反的顺序放置。

标签: javascript knex.js


【解决方案1】:

想通了。

这里,而不是 int

    table.int('userId').notNullable().references('id').inTable('users');

它应该是整数。

    table.integer('userId').notNullable().references('id').inTable('users');

【讨论】:

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