原始SQL中的Sequelize Postgres值始终在迁移期间抛出语法错误

时间:2017-09-14 22:02:07

标签: postgresql sequelize.js

我尝试使用双引号,单引号,转义引号,无引号,围绕CASE中的值,但每次运行迁移时都会有syntax error at or near "'example'"

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return Promise.resolve()
      // ... Removed for brevity
      .then(() => queryInterface
        .addColumn(...)
      )
      .then(() =>
        Sequelize.sequelize.query(
          'UPDATE TableName SET TableName.status=CASE ' +
          'WHEN TableName.status_old IS \'example\' THEN \'example2\' ' +
          'ELSE TableName.status_old END'
        )
      )
      .then(() => queryInterface
        .addColumn(...)
      );
  },

  down: (queryInterface, Sequelize) => {
    // ... Removed for brevity
  }
};

1 个答案:

答案 0 :(得分:1)

你的问题是IS,PostgreSQL不知道它是什么。您想要一个简单的=比较:

'UPDATE TableName SET TableName.status=CASE ' +
'WHEN TableName.status_old = \'example\' THEN \'example2\' ' +
'ELSE TableName.status_old END'