node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Decimal numbers are always returned as strings, not numbers

Open FredOleary opened this issue 8 years ago • 12 comments

Database decimal numbers E.g.decimal(19.2) are always return as strings rather than numbers. Even when numbers are small. E.g the number 123 is returned as "123". I've tried various combinations of supportBigNumbers and bigNumberStrings as per your documentation but nothing seems to work

FredOleary avatar Jun 12 '18 16:06 FredOleary

This is documented: https://github.com/sidorares/node-mysql2/tree/master/documentation#known-incompatibilities-with-node-mysql

My reasoning is that even small numbers when represented as float potentially serialized to string differently after string -> float -> string conversion so I keep initial string representation. It's up to you how to convert them.

What is the reason you have DECIMAL type in your db design @FredOleary ?

sidorares avatar Jun 12 '18 22:06 sidorares

Thanks for the quick response. I've inherited the database so have no choice in the DECIMAL choice however I do need to have numbers returned to the app consumers be returned as numbers, not strings. My work around is to use parseFloat() but really prefer not to since other developers certainly won't be expecting this. (I wasn't) I'll check out the 'decimalNumbers' you provided. (BTW your readme doc on "supportBigNumbers" and 'bigNumberStrings ' does indicate that some combination of these will allow numbers to be returned

FredOleary avatar Jun 12 '18 23:06 FredOleary

if you feel parts of documentation are misleading or confusing PR with fix is definitely welcome :)

( noticed almost identical discussion in #237 , linking it here)

One work around you can have is to use typeCast parameter like this:

var mysql = require("mysql");
var pool = mysql.createPool({
    user: "...",
    // ....
    typeCast: function (field, next) {
        if (field.type == "DECIMAL") {
            var value = field.string();
            return (value === null) ? null : Number(value);
        }
        return next();
    }
});

sidorares avatar Jun 12 '18 23:06 sidorares

Thanks... My bad on the documentation. I ended up on the wrong github from a search... Your documentation is quite clear on this

FredOleary avatar Jun 13 '18 00:06 FredOleary

Why was that design approved? I couldn't imagine case when having decimal from mysql as string is ok. Of course, in some situations maybe, but that's quitly uncommon. Why won't you change the default setting for DECIMAL to return as number?

vpotseluyko avatar Jul 19 '19 02:07 vpotseluyko

The reasoning is that DECIMAL type carries some special semantics and we want to preserve it. If someone who designed schema choose DECIMAL over regular number there might was a reason behind that

Also, the driver mostly just reflects what is sent at protocol level. In binary protocol ( prepared statements ) regular numbers are sent using IEEE 754 4 or 8 byte float but with DECIMAL number is sent as string

sidorares avatar Jul 19 '19 03:07 sidorares

Oh, understood. So the real question is why mysql nodejs driver giveaways decimal as number by default. Ok. I hope origins of decimal type are important enough for being added to readme.

Thanks for quick response)

vpotseluyko avatar Jul 19 '19 09:07 vpotseluyko

Wow I switched from mysql to mysql2 here, this seriously caught me out. This was a bad design decision.

jpike88 avatar Jun 05 '21 11:06 jpike88

Ran into this issue during mysql->mysql2 with a BIGINT column as well..

Brainicism avatar Jun 20 '21 20:06 Brainicism

if you feel parts of documentation are misleading or confusing PR with fix is definitely welcome :)

( noticed almost identical discussion in #237 , linking it here)

One work around you can have is to use typeCast parameter like this:

var mysql = require("mysql");
var pool = mysql.createPool({
    user: "...",
    // ....
    typeCast: function (field, next) {
        if (field.type == "DECIMAL") {
            var value = field.string();
            return (value === null) ? null : Number(value);
        }
        return next();
    }
});

Thanks for the solution. For MariaDB 10.4.26 I've had to use field.type == "NEWDECIMAL"

samdoidge avatar Jun 04 '23 18:06 samdoidge

like in documentation { decimalNumbers: true }

this is save myday, i got error on production server and parsing everything haha

haidarabdillah avatar Jun 23 '23 00:06 haidarabdillah

down the rabbit hole.

Manit-Chanthavong avatar Dec 22 '23 16:12 Manit-Chanthavong