30

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',
    }); 

6 Answers 6

46

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

knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }).toSQL().toNative()
5
  • 2
    To get query string is toNative().sql
    – Hill
    Dec 8, 2021 at 4:45
  • 1
    And to get parameter bindings for that query is toNative().bindings Dec 15, 2021 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) Jul 4, 2022 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. Jul 5, 2022 at 19:20
  • 1
    Link is broken. Now both toString() and toNative() are documented in knexjs.org/guide/interfaces.html#other. They both seem to be good for debugging purposes.
    – Tola
    Jul 27, 2022 at 7:37
19
knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }).toString();
4
  • I do not think this an improvement over the accepted answer.
    – greybeard
    Dec 15, 2020 at 21:55
  • 2
    Guys, I tried to find the way to get raw SQL. toString() is the same way as .toSQL().toNative() but shorter. Do you really think that there is one right answer only?
    – Alex Gusev
    Dec 16, 2020 at 5:00
  • 1
    This works in knex 0.21
    – James T.
    Dec 13, 2021 at 23:40
  • 2
    @greybeard I think it is an improvement
    – ibodi
    Jul 18, 2022 at 11:45
12

.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' ]
}
1
  • Not clear which things the OP wanted, but it's super useful to have these options laid out.
    – hartshoj
    Jun 25 at 19:44
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

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.

2

you can use toKnexQuery().toSQL()

console.log(query.toKnexQuery().toSQL())

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.