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

Why is it that `result.insertId` can be number or string when calling `insert()`

Open mateuscb opened this issue 8 years ago • 10 comments

The code and these tests: (number, string) make me think that result.insertId can be either a number or a string. If that is correct, what was the reasoning for doing that? It puts the burden on the caller to deal with both types.

If it is expected behavior, I think it would be nice to update the documentation to state that it could return a number or a string.

I have enabled both big number configs: {supportBigNumbers: true, bigNumberStrings: true}

mateuscb avatar Aug 22 '17 22:08 mateuscb

I would expect it to follow same rules as other numbers - number if can fit 53 bit, string if bigNumberStrings and supportBigNumbers

ref #108

sidorares avatar Aug 22 '17 22:08 sidorares

main reasoning is that mysql type accuracy can be such that it cannot fit into JS number.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

sidorares avatar Aug 22 '17 22:08 sidorares

Just to clarify: if it can fit in 53bit it will always be number, regardless if bigNumberStrings and supportBigNumbers are set. If it can't fit in 53bit it will return string if bigNumberStrings and supportBigNumbers are set, and error if they are not set?

Thanks for the quick response!

mateuscb avatar Aug 22 '17 22:08 mateuscb

Just saw your last comment. I get the accuracy limitation, my doubt is why not always return a string? That way the caller doesn't have to worry about it being a string or a number.

mateuscb avatar Aug 22 '17 22:08 mateuscb

yes, I kind of agree with you, I prefer to treat insertId as non-numeric unique id. Right now thie behaviour is made to be consistent with normal rows result where most of the time one prefer to have numbers as numbers. If we decide to change insertId behaviour that would require major version bump

sidorares avatar Aug 23 '17 00:08 sidorares

Doesn't the insertId behavior not follow the same rules? When the new row id is small, insertId gives me back a number, but when I select from that same row, the id comes back as a string. This is with bigNumberStrings and supportBigNumbers enabled. I would expect the id to be treated consistently, regardless of whether it came from an insert or select and consider this behavior to not be self-consistent.

pbomb avatar Aug 23 '17 14:08 pbomb

@pbomb I'm pretty sure the intention was to have this consistent, reopening to check and fix if not

sidorares avatar Aug 23 '17 23:08 sidorares

Will there be any movement on this issue? Thanks

dvisztempacct avatar Oct 03 '18 21:10 dvisztempacct

@dvisztempacct no movement, thank for bringing attention, would like to fix that

( any help to fix would be appreciated as well - in any form, even pr with failing test covering this behaviour )

sidorares avatar Oct 03 '18 21:10 sidorares

So is there any updated progress to solve this problem?

godlanbo avatar Nov 20 '23 11:11 godlanbo