Knex: How to do casting?

Created on 4 Mar 2016  路  3Comments  路  Source: knex/knex

select count(*)::integer from mytable
How to do this properly with knexjs?

question

Most helpful comment

knex('mytable').select(knex.raw('count(*)::integer'))

A more elegant solution to the big integer as string issue is detailed in #387.

All 3 comments

knex('mytable').select(knex.raw('count(*)::integer'))

A more elegant solution to the big integer as string issue is detailed in #387.

@rhys-vdw global Knex.raw is deprecated, use knex.raw (chain off an initialized knex object) (https://github.com/jfgodoy/knex-postgis/issues/20)

I updated it accordingly, using knex-postgis https://github.com/jfgodoy/knex-postgis/issues/20#issuecomment-312404336

Now the query: .select(st.asText('count(*)::integer'))
shows: column "count(*)::integer" does not exist

Using:
[email protected]
[email protected]
[email protected]

@sagidM knex.raw definitely exists. You just need to call it from configured knex instance, not from require('knex').raw. And about knex-postgis you should ask elsewhere.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fsebbah picture fsebbah  路  3Comments

marianomerlo picture marianomerlo  路  3Comments

PaulOlteanu picture PaulOlteanu  路  3Comments

mishitpatel picture mishitpatel  路  3Comments

rarkins picture rarkins  路  3Comments