如何在续集中将.update()值设置为NULL

jer*_*low 13 javascript database postgresql sequelize.js

我正在编写我的服务,使用PostGres的sequelize更新一行.当我使用PSequel试用我的查询时它工作正常:

UPDATE "test_table" SET "test_col"=NULL WHERE "id"= '2'
Run Code Online (Sandbox Code Playgroud)

但是使用sequelize会抛出500错误:

db.TestTable.update({ testCol: NULL }, { where: { id: id } })
  .then((count) => {
    if (count) {
      return count;
    }
  });
Run Code Online (Sandbox Code Playgroud)

我的模型确实允许null,我认为是允许空值为默认值以及设置:

testCol: {
  type: DataTypes.INTEGER,
  allowNull: true,
  defaultValue: null,
  field: 'test_col'
},
Run Code Online (Sandbox Code Playgroud)

除NULL之外的任何其他值都按预期工作.设置空值有不同的方法吗?

小智 10

从它的外观来看,我认为你的问题是你使用SQL的语法为空值('NULL'),你应该使用JS语法('null').

db.TestTable.update({ testCol: null }, { where: { id: id } })
  .then((count) => {
    if (count) {
      return count;
    }
  });
Run Code Online (Sandbox Code Playgroud)

应该管用.