Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select count(*) as c from users returns type "string" #39

Closed
bf opened this issue Sep 16, 2015 · 4 comments
Closed

select count(*) as c from users returns type "string" #39

bf opened this issue Sep 16, 2015 · 4 comments

Comments

@bf
Copy link

bf commented Sep 16, 2015

First of all thanks for this nice project. I am experiencing an issue where I use count(*) in a query and I get the value "2" returned, which is represented as a Javascript string.

An explicit typecast fixes the problem, but I am afraid that many more of these bugs might occur in the project. As this issue came up overnight, I am unsure where to search the culprit of this problem.

I have written test cases using db.connect -> db.query -> typeof result.rows[0].c === "number", and found out that only count(*) raises this issue. I am using pg version 4.4.1.

@bendrucker
Copy link
Collaborator

This is not a bug. Take a look at the postgres docs on the types of the aggregate functions:

http://www.postgresql.org/docs/9.4/static/functions-aggregate.html

You'll see that count is a bigint. node-postgres can't auto-parse that for you without loss of precision. If you know that your counts are small enough, you can safely parse it in your application code.

@bf
Copy link
Author

bf commented Sep 16, 2015

This issue appeared overnight and created failing comparisons like count === 2 which now needs to be either SQL-typecasted or parseInt(count, 10) === 2. I know about bigint conversion problems but in my case this issue arose in a mature project without me changing much before.

Now I am stuck trying to figure out if a postgres update caused this behavior, or some hidden node-pg update, or.. ?

@bendrucker
Copy link
Collaborator

No idea what changed in your setup but this has been the behavior in node-postgres for a long time, well over a year if memory serves.

@bf
Copy link
Author

bf commented Sep 16, 2015

Okay, thanks for the clarification. This lets me rule out a change in the node-pg modules. I still cannot wrap my head around this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants