TypeError:s.replace不是函数

时间:2018-02-19 16:10:55

标签: postgresql sequelize.js sequelize-cli

ENV:

  • Postgres:10.2
  • 节点:6.11.0
  • CLI:2.4.0
  • ORM:2.1.3

型号:

'use strict';
module.exports = function(sequelize, DataTypes) {
    var test = sequelize.define('test', {
        id: DataTypes.UUID,
        type: DataTypes.STRING,
        data: DataTypes.JSON,
    }, {
        'createdAt': {
            type: Sequelize.DATE(3),
            defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3)'),
        },
        'updatedAt': {
            type: Sequelize.DATE(3),
            defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'),
        },
        classMethods: {
            associate: function(models) {
                // associations can be defined here
            }
        }
    });
    return test;
}

迁移:

'use strict';
module.exports = {
    up: function(queryInterface, Sequelize) {
        return queryInterface.addColumn('test', {
            test_col: {
                type: Sequelize.JSONB
            }
        });
    },
    down: function(queryInterface, Sequelize) {
        return queryInterface.removeColumn('test', 'test_col');
    }
};

错误:

TypeError: s.replace is not a function
    at Object.removeTicks (/node_modules/sequelize/lib/utils.js:329:14)
    at Object.addTicks (/node_modules/sequelize/lib/utils.js:325:29)
    at Object.quoteIdentifier (/node_modules/sequelize/lib/dialects/postgres/query-generator.js:835:22)
    at Object.addColumnQuery (/node_modules/sequelize/lib/dialects/postgres/query-generator.js:182:19)
    at QueryInterface.module.exports.QueryInterface.addColumn (/node_modules/sequelize/lib/query-interface.js:358:53)
    at Object.up (/db/migrations/20180219152544-update.js:4:31)
    at constructor._exec (/node_modules/sequelize-cli/node_modules/umzug/lib/migration.js:103:23)
    at constructor.up (/node_modules/sequelize-cli/node_modules/umzug/lib/migration.js:69:17)
    at constructor.<anonymous> (/node_modules/sequelize-cli/node_modules/umzug/index.js:124:28)
    at PassThroughHandlerContext.finallyHandler (/node_modules/sequelize-cli/node_modules/bluebird/js/release/finally.js:56:23)
    at PassThroughHandlerContext.tryCatcher (/node_modules/sequelize-cli/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:510:31)
    at Promise._settlePromise (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:567:18)
    at Promise._settlePromise0 (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:612:10)
    at Promise._settlePromises (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:691:18)
    at Promise._fulfill (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:636:18)
    at Promise._resolveCallback (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:431:57)
    at Promise._settlePromiseFromHandler (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:522:17)
    at Promise._settlePromise (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:567:18)
    at Promise._settlePromise0 (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:612:10)
    at Promise._settlePromises (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:691:18)
    at Promise._fulfill (/node_modules/sequelize-cli/node_modules/bluebird/js/release/promise.js:636:18)

1 个答案:

答案 0 :(得分:2)

用于迁移的语法不正确。 queryInterface.addcolumn方法在您仅传递2时需要3个参数。请参阅here

public addColumn(table: String, key: String, attribute: Object, options: Object): Promise

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.addColumn(
          'pages',
          'group',
          Sequelize.JSONB
      );
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.removeColumn('pages', 'group');
  }
};

对任何得到s.replace错误的人来说,基本上是因为你没有传递预期的论证类型。因此,在上面的情况下,一个对象作为第二个参数传递给queryInterface.addColumn,而期望String字面值。阅读更多here