Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 783
Akshay Khetrapal Avatar asked Sep 13 '16 08:09

Akshay Khetrapal


People also ask

What does KNEX insert return?

Knex. raw insert does not return a number of rows inserted to the table. It returns empty array [] But knex.

What is Knexjs?

Knex.js (pronounced /kəˈnɛks/) is a "batteries included" SQL query builder for PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use.

Is KNEX an ORM?

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.

What is KNEX NPM?

Knex is a SQL query builder, mainly used for Node. js applications with built in model schema creation, table migrations, connection pooling and seeding.


2 Answers

Have you tried:

knex("users").whereNotNull("photo").groupBy("location")

like image 94
alex Avatar answered Oct 19 '22 23:10

alex


The docs have the answers. There is whereNull, whereNotNull, havingNull, havingNotNull and so on.

From the DOCS:

havingNull — .havingNull(column)
Adds a havingNull clause to the query.

knex.select('*').from('users').havingNull('email')

Outputs:

select * from `users` having `email` is null

havingNotNull — .havingNotNull(column)
Adds a havingNotNull clause to the query.

knex.select('*').from('users').havingNotNull('email')

Outputs:

select * from `users` having `email` is not null

Give it a try using the knex query lab: http://michaelavila.com/knex-querylab/

like image 39
Marco Avatar answered Oct 19 '22 22:10

Marco