后续迁移addColumn不使用环境变量

时间:2019-05-20 07:56:49

标签: node.js postgresql sequelize.js sequelize-cli

我想向带有迁移文件的现有表中添加列不在公共模式中,但是,当我在CLI中执行迁移时,它似乎忽略了描述模式。

我已经用sequelize migration:create --name 37-AddUserColumn.js --env "development"生成了新的迁移文件,但是当我使用sequelize db:migrate --env "development"运行迁移时,它失败了,因为它试图访问公共模式中的表而不是访问表中设置的模式中的表。 sequelize-cli config.js文件的“开发”环境。

config.js文件具有如下设置的“开发”环境:

"development": {
    "username": "my_user",
    "password": "my_pass",
    "database": "db_name",
    "host": "IP",
    "port": 5432,
    "dialect": "postgres",
    "schema": "my_schema",
    "migrationStorage": "json",
    "migrationStoragePath": "sequelizeMeta.json",
    "migrationStorageTableName": "sequelize_meta"
  }

,它在创建表或设置/更新索引时效果很好。 迁移文件为

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.addColumn({
      tableName: 'Urls',
    },
      'last_updated_by',
      {
        type: Sequelize.STRING,
        allowNull: true,
      });
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.removeColumn({
      tableName: 'Urls',
    },
      'last_updated_by',
    );
  }
};

由于某种原因,此迁移将在“公共”模式中而不是在“ my_schema”模式中查找“ Urls”表。完整的错误是(已删除路径)

> .sequelize db:migrate --env "development" --debug true

Sequelize CLI [Node: 10.9.0, CLI: 4.1.1, ORM: 4.38.1]

Loaded configuration file "db\config\database.json".
Using environment "development".
node_modules\sequelize\lib\sequelize.js:242:13
== 37-AddUserColumn: migrating =======

ERROR: SequelizeDatabaseError: relation "public.Urls" does not exist
    at Query.formatError (\api\node_modules\sequelize\lib\dialects\postgres\query.js:363:16)
    at query.catch.err (\api\node_modules\sequelize\lib\dialects\postgres\query.js:86:18)
    at tryCatcher (\api\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (\api\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (\api\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (\api\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (\api\node_modules\bluebird\js\release\promise.js:690:18)
    at _drainQueueStep (\api\node_modules\bluebird\js\release\async.js:138:12)
    at _drainQueue (\api\node_modules\bluebird\js\release\async.js:131:9)
    at Async._drainQueues (\api\node_modules\bluebird\js\release\async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (\api\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:693:18)
    at tryOnImmediate (timers.js:664:5)
    at processImmediate (timers.js:646:5)

我知道我可以在迁移文件中的addColumn中添加选项schema:'my_schema',并且可以使用它,但是我有几个使用不同模式的环境,因此必须将这个特定的列添加到所有环境中。不必为不同的架构创建不同的迁移不是答案。

有什么我想念的吗?与所有其他情况一样,该模式是否应该从config.js文件中获取?

0 个答案:

没有答案