Home > Software design >  Rails: Storing high currency values in PostgreSQL
Rails: Storing high currency values in PostgreSQL

Time:10-30

I would like to store high value currency values into the DB.

I tried to use an integer field in my migration, however I get following error

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

So I then tried to use decimal with precision

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

Once gain I am getting the same error when I enter

What I would like to know, is it possible, and how can I save a value like 1000000.0 into the database.

CodePudding user response:

I tried to use an integer field in my migration, however I get following error

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

That's a misunderstanding. The error message is for data type numeric - numeric(8,2) to be precise - not integer.

Solution

Just use numeric without precision and scale. It stores decimal number (with any amount of fractional digits) exactly as given.

decimal is a synonym of numeric in Postgres.

If you don't have fractional digits, consider integer (max 2^31 - 1) or bigint (max 2^63 - 1).

Related:

  • Related