Env:
Postgres:10.2
Node:6.11.0
单击: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)
最佳答案
用于迁移的语法不正确。queryInterface.addcolumn方法需要3个参数,而您只传递2个参数。见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。