Sequelize - PostgreSQL 数组查询中不包含字符串

BML*_*L91 6 javascript postgresql sequelize.js

我有一个名为 的 Sequelize 模型Staff。其上有一个数组字段,描述如下:

const Staff = sequelize.define("staff", {
    name: {
        type: DataTypes.STRING,
    },
    email: {
        type: DataTypes.STRING,
        unique: true
    },
    password: {
        type: DataTypes.STRING,
    },
    roles: {
        type: DataTypes.ARRAY(DataTypes.STRING)
    },
});
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个 GraphQL 端点,为所有在其领域没有“讲师”的员工提供服务roles

我已阅读本页文档,建议我使用Sequelize.Op. 所以我创建了这个:

return Staff.findAll({
        where: {
            roles: {
                [Sequelize.Op.not]: {[Sequelize.Op.contains]: ['instructor']},
            }
        }
    }
)
Run Code Online (Sandbox Code Playgroud)

上面抛出错误:

"message": "values.map is not a function"
Run Code Online (Sandbox Code Playgroud)

但是,如果我要尝试一个不是组合的查询,例如:

return models.Staff.findAll({
        where: {
            roles: {
                [Sequelize.Op.contains]: ['instructor']
            }
        }
    }
)
Run Code Online (Sandbox Code Playgroud)

查询运行正常,这让我相信我可能存在语法错误或对Op逻辑组合方式的误解。

小智 6

尝试这个:

return models.Staff.findAll({
  where: {
    $not: {
      roles: {
        $contains: ['instructor'],
      },
    },
  },
})
Run Code Online (Sandbox Code Playgroud)

该子句$not必须早于您的目标列。