You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sequelize中对PostgreSQL数组执行排除指定关键词的WHERE查询

Fixing Sequelize Query for Excluding PostgreSQL Array Elements

Hey there, let's sort out why your query is throwing that TypeError: values.map is not a function error and get it working correctly!

The Problem with Your Current Approach

Your keywords column is a PostgreSQL TEXT[] array type, but you're using Op.notLike—which is designed for string matching, not array operations. Sequelize tries to treat the array value like a string, hence the error when it attempts to run string-specific methods on an array.

The Correct Solutions

Since you're working with PostgreSQL arrays, we need to use array-specific operators that Sequelize supports for PostgreSQL.

Option 1: Use Op.notContains (Recommended)

Sequelize provides the Op.notContains operator which maps directly to PostgreSQL's NOT @> operator—this checks if an array does not contain the specified element(s).

Here's the fixed query:

const { Op } = require('sequelize'); // Don't forget to import Op first

const transactions = await db.transaction.findAll({
  where: {
    keywords: {
      [Op.notContains]: ['MyKey'] // Pass the target value as an array
    }
  }
}).catch(e => log.error(e));

Option 2: Use a Literal Query

If you prefer raw PostgreSQL syntax (or run into version-specific issues with Op.notContains), you can use sequelize.literal to execute direct database logic:

const transactions = await db.transaction.findAll({
  where: sequelize.literal('NOT (keywords @> ARRAY[\'MyKey\']::text[])')
}).catch(e => log.error(e));

Why This Works

PostgreSQL's @> operator checks if the left-hand array contains all elements of the right-hand array. Wrapping it in NOT inverts the condition, giving us all rows where keywords does not include 'MyKey'.

内容的提问来源于stack exchange,提问作者Karl Adler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:03:09