I suspect your use of orWhere
is negating your use of whereNot
. Without testing it, I would expect it to generate SQL a bit like the following:
SELECT * FROM posts
WHERE slug != 'whatever'
AND ...
AND ...
OR ...
OR ...
Note that there is no particular grouping to these clauses. All of them apply, and because some are OR
, there's no requirement that your slug
clause evaluates true.
One way around this is to use the Knex solution to grouping: pass a function instead of an object.
const posts = await knex('posts')
.where({
is_active: true,
type: excludeThis.type
})
.andWhere(qb =>
qb
.where('keywords', 'ilike', `%${excludeThis.keywords}%`)
.orWhere('title', 'ilike', `%${excludeThis.title}%`)
)
.andWhereNot({ slug })
This will generate SQL along the lines of:
SELECT * FROM posts
WHERE ...
AND ...
AND ( ... OR ... )
AND slug != 'whatever'
Here the OR
only applies within the group, so all the clauses outside the group must still evaluate true.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…