简体   繁体   English

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

[英]Sequelize - does not contain a string within a PostgreSQL array query

I have a Sequelize model called Staff .我有一个名为Staff的 Sequelize 模型。 On it there's an array field described as follows:上面有一个数组字段,描述如下:

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)
    },
});

I'm trying to create a GraphQL end point which serves up all staff which don't have 'instructor' in their roles field.我正在尝试创建一个 GraphQL 端点,该端点为所有在其roles字段中没有“讲师”的员工提供服务。

I've read this page of the docs , suggesting that I use a combination of Sequelize.Op .我已经阅读了文档的这个页面,建议我使用Sequelize.Op的组合。 So I created this:所以我创建了这个:

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

The above throws the error:以上抛出错误:

"message": "values.map is not a function"

However, if I am to try a query which isn't a combo such as:但是,如果我要尝试一个不是组合的查询,例如:

return models.Staff.findAll({
        where: {
            roles: {
                [Sequelize.Op.contains]: ['instructor']
            }
        }
    }
)

The query runs properly, this leads me to believe I perhaps have a syntax error or misunderstanding of how Op logic is combined.查询运行正常,这让我相信我可能对Op逻辑的组合方式有语法错误或误解。

Try this:尝试这个:

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

The clause $not need to be prior than the column you target.子句$not不需要在您定位的列之前。

This worked just fine for me:这对我来说很好用:

return models.Staff.findAll({
   where: {
    [Sequelize.Op.not]: {
      roles: {
       [Sequelize.Op.contains]: ['instructor'],
      },
    },
  },
})

Sequelize.Op.not have to come before the column that's being reffered to. Sequelize.Op.not不必出现在被Sequelize.Op.not的列之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM