Nodejs Sequelize 的 FindOne() 模型包含四舍五入或截断的值
FindOne() model for Nodejs Sequelize contains rounded or truncated values
我运行正在编写以下代码,其中 Tags 连接到我在本地设置的 sqlite 数据库。目前我有以下代码尝试从数据库中提取数据,作为测试。
console.log('debug after here.');
const tag = await Tags.findOne({ where: { guild: message.guild.id, channel: message.channel.id } });
console.log(' in guild: ' + message.guild.id);
console.log('out guild: ' + tag.guild);
console.log(' in chanl: ' + message.channel.id);
console.log('out chanl: ' + tag.channel);
console.log('out lpmID: ' + tag.latestPollMessageId);
结果如下
debug after here.
in guild: 228902712417189890
out guild: 228902712417189900
in chanl: 634445907059933195
out chanl: 634445907059933200
out lpmID: 754129896791474200
如您所见,输入公会和输入频道值与返回的公会和频道值不匹配。我查看了实际的 .sqlite 文件并看到了以下内容:
database values
如您所见,这些值并不代表我在 运行 .findOne() 时实际得到的结果,但我不知道为什么会这样。看起来它通常被四舍五入到最接近的百位,但我在测试中看到一些结果向下而不是向上舍入。
在JavaScript中,您最多只能存储 53 位数字。这意味着您可以以安全整数格式存储的最大数字是 9007199254740991。(请参阅 Number.MAX_SAFE_INTEGER)。
在模型定义中使用 id 作为字符串或数据类型作为 bigint
我推荐你阅读这篇文章What is JavaScript's highest integer value that a number can go to without losing precision?
我运行正在编写以下代码,其中 Tags 连接到我在本地设置的 sqlite 数据库。目前我有以下代码尝试从数据库中提取数据,作为测试。
console.log('debug after here.');
const tag = await Tags.findOne({ where: { guild: message.guild.id, channel: message.channel.id } });
console.log(' in guild: ' + message.guild.id);
console.log('out guild: ' + tag.guild);
console.log(' in chanl: ' + message.channel.id);
console.log('out chanl: ' + tag.channel);
console.log('out lpmID: ' + tag.latestPollMessageId);
结果如下
debug after here.
in guild: 228902712417189890
out guild: 228902712417189900
in chanl: 634445907059933195
out chanl: 634445907059933200
out lpmID: 754129896791474200
如您所见,输入公会和输入频道值与返回的公会和频道值不匹配。我查看了实际的 .sqlite 文件并看到了以下内容:
database values
如您所见,这些值并不代表我在 运行 .findOne() 时实际得到的结果,但我不知道为什么会这样。看起来它通常被四舍五入到最接近的百位,但我在测试中看到一些结果向下而不是向上舍入。
在JavaScript中,您最多只能存储 53 位数字。这意味着您可以以安全整数格式存储的最大数字是 9007199254740991。(请参阅 Number.MAX_SAFE_INTEGER)。
在模型定义中使用 id 作为字符串或数据类型作为 bigint
我推荐你阅读这篇文章What is JavaScript's highest integer value that a number can go to without losing precision?