【问题标题】:Unable to execute the raw query in Sequelize migrations无法在 Sequelize 迁移中执行原始查询
【发布时间】:2018-08-21 11:02:23
【问题描述】:

我正在尝试使用 Sequelize 迁移更新我的数据库,因此我尝试编写这样的 Sequelize 迁移

'use strict';
module.exports = {
  up: (queryInterface, Sequelize, migration) => {
    queryInterface.addColumn('coaching_class_entries', 'bill_cycle', {
      type: Sequelize.INTEGER(4),
      allowNull: false,
      defaultValue: '0',
      field: 'bill_cycle',
      after: 'fees'
    })
      .then(() => queryInterface.addColumn('coaching_classes', 'bill_plans', {
        type: Sequelize.JSON,
        allowNull: false,
        defaultValue: 'NULL',
        field: 'bill_plans',
        after: 'bill_cycle'
      }))
      .then(() =>
        migration.migrator.Sequelize.query('UPDATE coaching_classes  SET bill_plans = JSON_ARRAY(JSON_OBJECT("cycle", bill_cycle, "fee", fees));'));

  },

  down: (queryInterface, Sequelize) => {
    let migrations = [];

    migrations.push(queryInterface.removeColumn('coaching_class_entries', 'bill_cycle'))
    migrations.push(queryInterface.removeColumn('coaching_classes', 'bill_plans'))
    return Promise.all(migrations);

  }
};

但它总是在原始查询行中给我错误

无法读取未定义的属性“Sequelize”

正确的语法是什么?

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    我自己发现的,只有我们必须使用简单的queryInterface.sequelize.query

    【讨论】:

    • 我试过这个并为我工作。我面临的问题是它返回我转换为2019-10-01T08:00:00.000Z 的时间字段,但我只想要2019-10-01 08:00:00。我在 Db 中为该列所拥有的是 2019-10-01 08:00:00 我想要相同的结果。你有什么解决办法吗?如果我在 mysqlworkbench 中运行相同的查询,我会得到与存储在数据库中相同的结果,但是当我使用 queryInterface.sequelize.query 时,我会得到另一个。
    猜你喜欢
    • 2019-01-27
    • 2019-01-27
    • 2014-02-21
    • 2019-03-28
    • 2019-04-27
    • 2016-01-30
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多