I have a BINARY
field in my table which I usually grab like this:
SELECT HEX(users.id) AS id FROM users WHERE username = ?
I recently started using Knex because I need to be able to dynamically generate WHERE
clauses from objects. Here's what I tried:
knex('users').select('HEX(users.id) AS id)').where(filter);
Here's the query it generates:
select `HEX(users`.`id)` as `id` ....
And then I tried this:
knex('users').select('HEX(`users`.`id`) AS id').where(filter);
And it comes up with this:
select `HEX(``users```.```id``)` as `id` ....
How do I execute HEX()
without it being mistaken for a column name?
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.
Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.
Knex. raw insert does not return a number of rows inserted to the table. It returns empty array [] But knex.
The knex. schema is a getter function, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the knex. schema for every query. These methods return promises.
With knex letting to do quoting of identifiers it would look like this:
knex('users').select(knex.raw('HEX(??) AS id', ['users.id'])).where(filter);
I've found a solution. I have to use raw()
function. So my query builder will look like this:
knex('users').select(knex.raw('HEX(`users`.`id`) AS id')).where(filter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With