Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres sequelize raw query to get count returns string value

I am using postgresql sequelize in my node-express server.

When I run the following SQL Command, I am getting a string value for result.

I want to get Integer value for this count.

SELECT count(id) from collaborators where id<3

Result:

count =  [ { count: '1' } ]

Is there any way to get number values for result? Or do I always need to use parseInt(count, 10) to get int value?

I appreciate any help!

like image 212
wagng Avatar asked Jun 10 '26 04:06

wagng


2 Answers

@DemtriOS is correct that count returns bigint hence interpreted into string, you can directly typecast it directly on your query like so:

SELECT COUNT(id)::int
FROM collaborators
WHERE id < 3
like image 152
Miko Chu Avatar answered Jun 11 '26 18:06

Miko Chu


According to this https://www.postgresql.org/docs/8.2/static/functions-aggregate.html, the count() function returns bigint type. Because of this it will get interpreted as a string. It looks like explicit conversion to an int is what you will have to do. So, parseInt(count, 10) it is.

like image 32
DemetriOS Avatar answered Jun 11 '26 17:06

DemetriOS