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

Postgres BigInt Returned As String #4064

Closed
careygister opened this issue Apr 4, 2017 · 5 comments
Closed

Postgres BigInt Returned As String #4064

careygister opened this issue Apr 4, 2017 · 5 comments

Comments

@careygister
Copy link

careygister commented Apr 4, 2017

Sails version: 0.12.13
Node version: v6.9.4
NPM version: 3.10.10
Operating system: 4.9.13-moby Linux x86_64


I am using an existing set of Postgres tables in my Sails models. The tables have BigInt datatypes. I specified 'integer' as the type, assuming that Javascript native integers or numbers were used. I found that the returned datatype from a 'find' was a string. How can I model a Bigint?

Note: the native precision of Javascript numbers permits the representation of a Postgres Bigint.

Thank you.

@sailsbot
Copy link

sailsbot commented Apr 4, 2017

Hi @careygister! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:

  • Verify "I am experiencing a concrete technical issue (aka a bug) with Sails (ideas and feature proposals should follow the guide for proposing features and enhancements (http://bit.ly/sails-feature-guide), which involves making a pull request). If you're not 100% certain whether it's a bug or not, that's okay--you may continue. The worst that can happen is that the issue will be closed and we'll point you in the right direction."

As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact inquiries@sailsjs.com

@careygister
Copy link
Author

Ok, fixed!

@sailsbot
Copy link

sailsbot commented Apr 4, 2017

@careygister Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

@wulfsolter
Copy link
Contributor

I had a play with this a while back and as annoying as this is, after deeper research I do believe this is the correct behavior.

JavaScript's Number.MAX_SAFE_INTEGER is 9007199254740991 = 2^53-1.
Postgres' BigInt is up to 9223372036854775807 = 2^64 which is 1024x larger

There's a very real possibility for Postgres' BigInt to overflow JavaScript. When using numbers > MAX_SAFE_INTEGER, no errors or warnings are thrown (AFAIK) but the value is no longer true. Try it yourself by pasting Postgres' BigInt max into the console.

With that in mind, I guess I can't help but feel that I'd rather my ORM return an accurate string representation and leave it to me to typecast or manipulate using a library such as big-integer or bignum. BigInts as strings is pretty common in JS land, for example (twitter's JSON API returns the 64-bit int ID both as number and as string)[https://dev.twitter.com/overview/api/tweets] so you still have access to the correct ID.

@particlebanana
Copy link
Contributor

Yup that's correct. It's actually happening at the node-pg level. I think they expose their parsers if you really wanted to override it though.

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

No branches or pull requests

5 participants