为什么我的Knex迁移在Heroku上失败而在本地失败?

时间:2019-10-26 16:05:48

标签: heroku knex.js

我正在尝试在Heroku上部署我的第一个React应用程序,并且一切正常,直到进行迁移为止。我的Knex迁移在本地运行良好,但是当我尝试在Heroku上运行时,迁移失败。

由于在本地计算机上运行时它工作正常,而且这是我第一次尝试部署任何东西,所以我不确定如何调试它。

这是我的迁移代码:

exports.up = function(knex, Promise) {
  return Promise.all([
    // USERS TABLE
    knex.schema.createTable('users', t => {
      t.uuid('id')
        .primary()
        .unique()
        .notNullable();
      t.string('name').notNullable();
      t.string('email')
        .unique()
        .notNullable();
      t.string('password').notNullable();
      t.timestamp('joined', { useTz: false }).notNullable();
      t.text('website');
      t.string('github');
      t.string('twitter');
      t.text('avatar');
      t.boolean('is_admin')
        .defaultTo(false)
        .notNullable();
    }),

    // INSTRUCTORS TABLE
    knex.schema.createTable('instructors', t => {
      t.uuid('id')
        .primary()
        .unique()
        .notNullable();
      t.string('name')
        .unique()
        .notNullable();
      t.timestamp('created', { useTz: false }).notNullable();
      t.text('website');
      t.string('github');
      t.string('twitter');
      t.text('avatar');
    }),

    // TUTORIALS TABLE
    knex.schema.createTable('tutorials', t => {
      t.string('id')
        .primary()
        .unique()
        .notNullable();
      t.uuid('user_id').notNullable();
      t.uuid('instructor_id');
      t.string('instructor_name');
      t.string('title').notNullable();
      t.text('url')
        .unique()
        .notNullable();
      t.timestamp('date', { useTz: false }).notNullable();
      t.enum('cost', ['free', 'paid']).notNullable();
      t.enum('medium', ['article', 'video']).notNullable();
      t.enum('difficulty', ['beginner', 'advanced']).notNullable();
      t.specificType('categories', 'text ARRAY').notNullable();
    }),

    // COMMENTS TABLE
    knex.schema.createTable('comments', t => {
      t.string('id')
        .primary()
        .unique()
        .notNullable();
      t.uuid('user_id').notNullable();
      t.string('tutorial_id').notNullable();
      t.text('body').notNullable();
      t.timestamp('date', { useTz: false }).notNullable();
    }),

    knex.schema.createTable('tutorial_votes', t => {
      t.uuid('id')
        .primary()
        .unique()
        .notNullable();
      t.string('tutorial_id').notNullable();
      t.uuid('user_id').notNullable();
      t.smallint('vote_value');
    }),
    knex.schema.createTable('comment_votes', t => {
      t.uuid('id')
        .primary()
        .unique()
        .notNullable();
      t.string('comment_id').notNullable();
      t.uuid('user_id').notNullable();
      t.smallint('vote_value');
    }),
    knex.schema.createTable('favorites', t => {
      t.uuid('id')
        .primary()
        .unique()
        .notNullable();
      t.string('tutorial_id').notNullable();
      t.uuid('user_id').notNullable();
      t.timestamp('date', { useTz: false }).notNullable();
    })
  ]);

  console.log('Tables created successfully');
};

exports.down = function(knex, Promise) {
  return Promise.all([
    knex.schema.dropTable('users'),
    knex.schema.dropTable('instructors'),
    knex.schema.dropTable('tutorials'),
    knex.schema.dropTable('comments'),
    knex.schema.dropTable('tutorial_votes'),
    knex.schema.dropTable('comment_votes'),
    knex.schema.dropTable('favorites')
  ]);

  console.log('Tables dropped');
};

这是我得到的错误:

remote:        migration file "20190731184441_setup.js" failed
remote:        migration failed with error: Cannot read property 'all' of undefined
remote: TypeError: Cannot read property 'all' of undefined
remote:     at Object.exports.up (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/db/migrations/20190731184441_setup.js:2:18)
remote:     at Object.<anonymous> (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/lib/migrate/Migrator.js:503:40)
remote:     at Object.tryCatcher (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/util.js:16:23)
remote:     at Promise._settlePromiseFromHandler (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/promise.js:547:31)
remote:     at Promise._settlePromise (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/promise.js:604:18)
remote:     at Promise._settlePromiseCtx (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/promise.js:641:10)
remote:     at _drainQueueStep (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/async.js:97:12)
remote:     at _drainQueue (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/async.js:86:9)
remote:     at Async._drainQueues (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/async.js:102:5)
remote:     at Immediate.Async.drainQueues [as _onImmediate] (/tmp/build_7917ecdef592ca80a65bfdee9b4e67c6/server/node_modules/knex/node_modules/bluebird/js/release/async.js:15:14)
remote:     at processImmediate (internal/timers.js:439:21)

2 个答案:

答案 0 :(得分:0)

通过从Promiseexports.up = function(knex, Promise)中删除exports.down = function(knex, Promise)来解决

答案 1 :(得分:0)

我认为这很可能是本地版本和Heroku安装版本之间的版本不匹配。如果您的semver规范相当宽松,那将允许Heroku安装更新的版本,也许您已经在针对本地版本进行了一段时间的开发。

我怀疑的原因是:Knex switched to native promises在0.18.0版中,这意味着迁移不再通过Bluebird Promise。该参数不再存在,因此会出错。