简体   繁体   中英

How to perform where query for postgres Array in Sequelize

I have a model which defines a column keywords as an array of Strings. Now I wan't to query for all entries that do not contain a special keyword in the keywords column. My approach doesn't work like expected.

Modell:

export default function (sequelize, DataTypes) {
    return sequelize.define('transaction', {
        id: {
            primaryKey: true,
            type: DataTypes.INTEGER,
            autoIncrement: true
        },

        keywords: {
            type: DataTypes.ARRAY(DataTypes.TEXT),
            defaultValue: []
        },
        createdAt: DataTypes.DATE,
        updatedAt: DataTypes.DATE
    });
}

Query:

const transactions = await db.transaction.findAll({
    where: {keywords: {[Op.notLike]: '%MyKey%'}}
}).catch(e => log.error(e));

// Throws error: TypeError: values.map is not a function

Dependencies:

"pg": "6.4.1",
"pg-hstore": "^2.3.2",
"sequelize": "^4.28.8",

I am not sure there actually is an Array function/operator to do this within Postgresql. I can only see contains

https://www.postgresql.org/docs/9.1/static/functions-array.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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