Coder Social home page Coder Social logo

Comments (6)

abenhamdine avatar abenhamdine commented on August 27, 2024 1

For the future reader : in postgres, sum aggregate function determine its type as following (per docs https://www.postgresql.org/docs/current/static/functions-aggregate.html) :

bigint for smallint or int arguments, numeric for bigint arguments, otherwise the same as the argument data type

So even if you don't expect bigints or numerics from you db, you are likely to get ones soon or late if you use sum() and you should probably cast bigint and numerics as explained in the readme of this repo

from node-pg-types.

bendrucker avatar bendrucker commented on August 27, 2024

1700 should be a number (

return ((sign === 0) ? 1 : -1) * Math.round(result * scale) / scale;
) for sure. Can you double check that your column is that type?

https://www.postgresql.org/docs/current/static/datatype-oid.html

from node-pg-types.

abenhamdine avatar abenhamdine commented on August 27, 2024

Yes, I've checked with the following query:

select attname, atttypid from pg_attribute
where attrelid = 'myTable'::regclass and attname = 'myColumn';

and the atttypid of 'myColumn' is definitely 1700.

Could this be a bug in node-postgres rather than in node-pg-types ?

from node-pg-types.

bendrucker avatar bendrucker commented on August 27, 2024

Actually I misread the source. Here's the test:

var bignum = '31415926535897932384626433832795028841971693993751058.16180339887498948482045868343656381177203091798057628'
exports.numeric = {
format: 'text',
id: 1700,
tests: [
[bignum, bignum]
]
}

numeric should be returned as a string:

The type numeric can store numbers with a very large number of digits and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required. However, arithmetic on numeric values is very slow compared to the integer types, or to the floating-point types described in the next section.

https://www.postgresql.org/docs/9.3/static/datatype-numeric.html

from node-pg-types.

abenhamdine avatar abenhamdine commented on August 27, 2024

Ah yes, I saw the test too, thx a lot.
Are you open to a PR to update the docs in order to clarify this ?

from node-pg-types.

bendrucker avatar bendrucker commented on August 27, 2024

Happy to review a PR. The thinking behind the bigint example is that it's returned from the count and so a lot of people encounter it without realizing and without ever using a non-JS-safe type in their schema. That wouldn't be true of this case but if you can find a concise way to note it we can add something.

from node-pg-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.