[Django]-A field with precision 10, scale 2 must round to an absolute value less than 10^8

33๐Ÿ‘

As @sivakumar-r mentioned above, itโ€™s the precision and scale issue.

Letโ€™s see how the Numeric(precision, scale) works:

Suppose, if we have a column defined with NUMERIC(10,3).

This means, we can only save 10 digits in total with 3 spaces after decimal point.

So the range you can save in this case is:
+9999999.999 to -9999999.999.

One Solution Could be:

Alter table column to use type NUMERIC without the precision and scale.

This will accept up to 131072 digits before the decimal point; up to 16383 digits after the decimal point. As mentioned in postgres:11 Numeric types document.

๐Ÿ‘คbh4r4th

3๐Ÿ‘

Earlier you had decimal number like Number(300, 100). I think you have some data in this decimal field. Now you alter the decimal field to Number(10,2). So now you can only save max 99999999.99. But your old data is much more than this. Because earlier you can save decimal number of 300 max digits and 100 decimal places.

๐Ÿ‘คSivakumar R

1๐Ÿ‘

Reproduce this postgresql error:

ERROR:  numeric field overflow 
A field with precision 5, scale 4 must round to an absolute 
value less than 10^1

Create a postgresql table with a column with type: numeric(4,4) then try to insert data from another table that has a numeric(5,4) column type:

CREATE TABLE mytable( mycolumn numeric(5,4)); 
insert into mytable values(12.3456); 
CREATE TABLE mytable2( mycolumn numeric(4,4)); 
insert into mytable2 ( select * from mytable ); --Error thrown here

Solution

Increase the space allotted to the numeric column receiving the data or define a custom cast function to make the number fit into its destination.

๐Ÿ‘คEric Leschinski

Leave a comment