Sequelize - does not contain a string within a PostgreSQL array query

自古美人都是妖i 提交于 2019-12-11 06:33:59

问题


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

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

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

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.


回答1:


Try this:

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

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



来源:https://stackoverflow.com/questions/52954784/sequelize-does-not-contain-a-string-within-a-postgresql-array-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!