0

I'm trying to change the data type of a Django model field from PositiveBigIntegerField() into DecimalField().

From:

quantity=models.PositiveBigIntegerField()

To:

quantity=models.DecimalField(max_digits=8, decimal_places=2)

But when I migrate the change, it shows the following Postgres DB error:

Numeric field overflow Detail: A field with precision 8, scale 2 must round to an absolute value less than 10^6.

is there any idea how can I fix this issue?

DEV
  • 65
  • 9
  • Try to go into the shell and make sure non the current quantity values stored are more than 8 digits and 2 decimal places. If so, change them, and try again – Nealium May 23 '23 at 19:12
  • From the docs [Numeric](https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL): *The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point.*. This means with a `decimal_place/scale` of `2` the longest integer that would work is `6` digits long. I'm guessing you have bigger integers then that. Do `select max(int_fld) from table` to see how big. – Adrian Klaver May 23 '23 at 19:19
  • @Nealium, actually precision of 8 means the total number of digits on either side of the decimal point. – Adrian Klaver May 23 '23 at 19:20
  • @AdrianKlaver I was just going off the Django field, not the postgres error. 8 numbers total, two of them are decimals places. digit == numbers in my mind – Nealium May 23 '23 at 19:27
  • Examples: `select 12345678::numeric(6, 2); ERROR: numeric field overflow DETAIL: A field with precision 6, scale 2 must round to an absolute value less than 10^4.` **vs** `select 12345678::numeric(10, 2); 12345678.00`. – Adrian Klaver May 23 '23 at 19:35

0 Answers0