Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a django-field total_price in postgres database with version 9.3.11.

Here is the current code for it.

total_value = models.DecimalField(decimal_places=100, default=0, max_digits=300)

I want to convert it to proper 2 decimal place. So I wrote this code

total_value = models.DecimalField(decimal_places=2, default=0, max_digits=10)

My migration file

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('my_table', '0040_my_table_skipped'),
    ]

    operations = [
        migrations.AlterField(
            model_name='my_table',
            name='total_value',
            field=models.DecimalField(default=0, max_digits=10, decimal_places=2),
        ),
    ]

When I run command python manage.py migrate

It shows me this error

A field with precision 10, scale 2 must round to an absolute value less than 10^8.

Any idea what is this about ?

like image 661
Adil Malik Avatar asked Nov 27 '19 08:11

Adil Malik


People also ask

What is precision and scale in Postgres?

PostgreSQL supports the NUMERIC type for storing numbers with a very large number of digits. Generally NUMERIC type are used for the monetary or amounts storage where precision is required. Syntax: NUMERIC(precision, scale) Where, Precision: Total number of digits. Scale: Number of digits in terms of a fraction.

What is scale and precision in decimal?

Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2. In SQL Server, the default maximum precision of numeric and decimal data types is 38.

What does numeric field overflow mean?

What does the error 'Numeric Field Overflow' mean? Cause: This error message can occur for a couple reasons: 1: The table or spreadsheet being imported has exceeded the maximum number of characters allowed in an Atlas .

What is double precision in PostgreSQL?

The double precision type has a range of around 1E-307 to 1E+308 with a precision of at least 15 digits. Values that are too large or too small will cause an error. Rounding might take place if the precision of an input number is too high.


Video Answer


2 Answers

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.

like image 126
bh4r4th Avatar answered Oct 07 '22 19:10

bh4r4th


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.

like image 32
Sivakumar R Avatar answered Oct 07 '22 20:10

Sivakumar R