Akshay Khetrapal Akshay Khetrapal - 5 months ago 43
Node.js Question

How to use IS NOT NULL in Knex JS

I am trying to create the following query using knex:

SELECT * FROM users group by users.location having users.photo is not null

as follows:

knex("users").groupBy("users.location").having("users.photo", "IS NOT", "Null")

I am getting the following error on this:

The operator IS NOT is not permitted

I've gone through their documentation and couldn't find anything useful.


According to the docs, .havingRaw is what you need:

knex("users").groupBy("users.location").havingRaw("users.photo IS NOT ?, [null]);

On the other hand, do a knex.raw at once unless there is any remaining advantage using the builder on this specific case.