27

For debugging purposes, I want to see the SQL query knex is executing. For example, I want to view SQL that knex produces for this code:

 knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }); 
Dan
  • 55,715
  • 40
  • 116
  • 154

6 Answers6

42

https://knexjs.org/guide/interfaces.html#other

knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }).toSQL().toNative()
Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
  • 2
    To get query string is `toNative().sql` – Hill Dec 08 '21 at 04:45
  • 1
    And to get parameter bindings for that query is `toNative().bindings` – Mikael Lepistö Dec 15 '21 at 11:18
  • This does not return the SQL command as a string, it returns an object. To get the raw SQL as a string, you want `.toString()` or `.toQuery()` (see my answer as it is more comprehensive) – Jeremy Bernier Jul 04 '22 at 22:20
  • @JeremyBernier No... if you call `.toString()` / `.toQuery()` that is not the query that knex runs when the builder is triggered. Outputted string might be working SQL some times and sometimes it is not. Knex does not do value escaping correctly in every case when rendering string from builder. `.toQuery()` and `.toString()` are aliases to each other. Both should be used only for debugging. `.toSQL().toNative()` returns the templated SQL query string for the used dialect and corresponding bindings. That should be the exact query that is executed by the knex when the builder is triggered. – Mikael Lepistö Jul 05 '22 at 19:20
  • 1
    Link is broken. Now both toString() and toNative() are documented in http://knexjs.org/guide/interfaces.html#other. They both seem to be good for debugging purposes. – Tola Jul 27 '22 at 07:37
18
knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }).toString();
Alex Gusev
  • 1,526
  • 3
  • 16
  • 35
10

.toString() and .toQuery() return the full SQL query as a string

For your example:

knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }).toString();

returns:

delete from "statistics" where "stats" = 'reddit'

Whereas .toSQL().toNative() returns an object:

knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }).toSQL().toNative();
{
  sql: 'select * from "books" where "author" = $1',
  bindings: [ 'John Doe' ]
}
Jeremy Bernier
  • 1,746
  • 1
  • 17
  • 17
4

To print all queries then passing a debug: true flag on your initialization object will turn on debugging for all queries.

knex({
    client: 'mysql',
    debug: true,
    connection: {
        host: '127.0.0.1',
        port: 3306,
        user: 'root',
        password: '',
        database: ''
    }
})

Ref:https://knexjs.org/#Installation-debug

John Lobo
  • 14,355
  • 2
  • 10
  • 20
3

In my case toSQL()... does not produce the "parsed" SQL string, only toString() works, I'm not sure if this depends on one's specific usage of the Query Builder.

1

you can use toKnexQuery().toSQL()

console.log(query.toKnexQuery().toSQL())
Ali Faris
  • 17,754
  • 10
  • 45
  • 70