Sequelize-PostgreSQL数组查询中不包含字符串 [英] Sequelize - does not contain a string within a PostgreSQL array query

查看:1001
本文介绍了Sequelize-PostgreSQL数组查询中不包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Sequelize model called Staff. 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)
    },
});

我正在尝试创建一个GraphQL端点,该端点可以为所有没有'讲师在其角色字段中。

I'm trying to create a GraphQL end point which serves up all staff which don't have 'instructor' in their roles field.

我已阅读文档的此页面,建议我使用 Sequelize.Op 的组合。所以我创建了这个:

I've read this page of the docs, suggesting that I use a combination of Sequelize.Op. So I created this:

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

以上抛出错误:

"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']
            }
        }
    }
)

查询正常运行,这使我相信我可能在语法上有误或对<$的误解c $ c> Op 逻辑相结合。

The query runs properly, this leads me to believe I perhaps have a syntax error or misunderstanding of how Op logic is combined.

推荐答案

尝试一下:

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

子句 $ not 优先于您定位的列。

The clause $not need to be prior than the column you target.

这篇关于Sequelize-PostgreSQL数组查询中不包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆