如何在Sequelize中对PostgreSQL数组执行排除指定关键词的WHERE查询
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

