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

count field filled with string at runtime, even though type is number #117

Closed
danielbrauer opened this issue Jun 27, 2020 · 6 comments
Closed

Comments

@danielbrauer
Copy link

danielbrauer commented Jun 27, 2020

I've got a query defined like this:

/* @name CountByName */ SELECT COUNT(*) FROM users WHERE username = :username;

But at runtime it's returning [{count:"0"}].

What might I be doing wrong?

danielbrauer added a commit to danielbrauer/instead that referenced this issue Jun 27, 2020
@adelsz
Copy link
Owner

adelsz commented Jun 27, 2020

In PG SQL queries count is returned as bigint.
PG's bigint can be bigger than JS's Number.MAX_SAFE_INTEGER so it is parsed as string by node-postgres.
If you are sure that your counts will fit into int4 then a potential workaround is to typecast the count to int in your query:

SELECT COUNT(*)::int FROM users WHERE username = :username; 

@danielbrauer
Copy link
Author

That makes sense, thank you! Would it make sense to do something like this? brianc/node-pg-types#78

@danielbrauer
Copy link
Author

Also, is it a bug that the type is number if it ends up being string at runtime?

@adelsz
Copy link
Owner

adelsz commented Jun 27, 2020

That makes sense, thank you! Would it make sense to do something like this? brianc/node-pg-types#78

Yes it makes sense, but currently we do not offer a way to override pgTyped's default type maps if you decide to use custom type parsers with node-postgres.
We plan to provide automatic BigInt conversion once we integrate with node-postgres more tightly.

Also, is it a bug that the type is number if it ends up being string at runtime?

Yes, please open an issue if that happened. Might be a mapping mistake somewhere in our type map config.

@danielbrauer
Copy link
Author

Yes, please open an issue if that happened. Might be a mapping mistake somewhere in our type map config.

Ah, that was the original reason for this issue. I was checking whether count === 0 and it didn't because it was a string.

@adelsz
Copy link
Owner

adelsz commented Jun 30, 2020

Ok, If I understood you correctly, casting COUNT(*)::int solved the issue you were having.
Closing this issue, let me know that is not the case.

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