【问题标题】:Rails: Storing high currency values in PostgreSQLRails:在 PostgreSQL 中存储高货币价值
【发布时间】:2021-10-28 21:21:19
【问题描述】:

我想将高价值货币值存储到数据库中。

我尝试在迁移中使用 integer 字段,但出现以下错误

PG::NumericValueOutOfRange: ERROR: numeric field overflow 
DETAIL: A field with precision 8, scale 2 must round to an absolute value less than 10^6.

然后我尝试使用精确的小数

t.decimal     :value, precision: 30, scale: 2

一旦获得,当我进入时我得到同样的错误

我想知道,有没有可能,以及如何将1000000.0 之类的值保存到数据库中。

【问题讨论】:

    标签: postgresql ruby-on-rails-6


    【解决方案1】:

    我尝试在迁移中使用整数字段,但出现以下错误

    PG::NumericValueOutOfRange: ERROR: numeric field overflow  
    DETAIL: A field with precision 8, scale 2 must round to an absolute value less than 10^6.
    

    这是个误会。错误消息是针对数据类型numeric - 准确地说是numeric(8,2) - 不是integer

    解决方案

    只需使用numeric without precision and scale。它完全按照给定的方式存储十进制数(带有任意数量的小数位数)。

    decimal 是 Postgres 中 numeric 的同义词。

    如果您没有小数位,请考虑 integer(最大 2^31 - 1)或 bigint(最大 2^63 - 1)。

    相关:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多