Postgres DatabaseError [SequelizeDatabaseError]: "IN" 处或附近的语法错误

Postgres DatabaseError [SequelizeDatabaseError]: syntax error at or near "IN"

我有端点应该从数据库中删除记录:

  delete: async(roleId, actionId) => {
    const actionrole = await ActionRoleModel.findAll({
      where: {
        roleId: roleId,
        actionId: actionId
      },
    });
    return await actionrole[0].destroy();
  }

[0] 必须在这里,因为 actionrole 看起来像 [{...}]。这是模型:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var ActionRole = sequelize.define('actionroles', {
    actionId: {
      type: "UNIQUEIDENTIFIER",
      field: "actionid"
    },
    roleId: {
      type: "UNIQUEIDENTIFIER",
      field: "roleid"
    },
    createdAt: {
      field: "createdat",
      type: DataTypes.DATE
    },
    updatedAt: {
      field: "updatedat",
      type: DataTypes.DATE
    },
  }, {});
  ActionRole.associate = function(models) {
    // associations can be defined here
  };

  ActionRole.removeAttribute('id');

  return ActionRole;
};

但作为终端错误,我得到

DatabaseError [SequelizeDatabaseError]: syntax error at or near "IN"

这里是 SQL:

DELETE FROM "actionroles" 
WHERE  IN (
  SELECT  FROM "actionroles" 
  WHERE "roleid" = '53549d62-cd2a-497f-9d1c-1ee1901261ab' AND "actionid" = '6c70bf65-30fd-4640-91d0-8fbda85c4dd5' 
  LIMIT 1)

怎么了?我该如何解决?

对于使用 Sequelize 版本 3 及更高版本的任何人来说,它看起来像:

Model.destroy({
    where: {
        // conditions
    }
})

所以,在这种情况下,它看起来像这样:

return await ActionRoleModel.destroy({
      where: {
        roleId: roleId,
        actionId: actionId
      }
    });

而且有效!