如何在Sequelize中查询postgres数组的位置

时间:2018-01-10 13:54:05

标签: node.js postgresql sequelize.js

我有一个模型,它将列keywords定义为字符串数组。现在,我不想查询keywords列中不包含特殊关键字的所有条目。我的方法并不像预期的那样有效。

型号名称:

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

查询:

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

依赖关系:

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

1 个答案:

答案 0 :(得分:0)

我不确定在Postgresql中是否有一个Array函数/运算符。我只能看到包含

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