迁移错误:Django / Postrgres:DataError数值精度10000必须在1到1000之间

时间:2019-04-12 16:43:09

标签: django database postgresql docker

第一次尝试将更改迁移到我的Postgresql DB时,遇到此错误。

使用DJango随附的标准SQLite数据库时,我可以成功迁移并启动我的应用程序。但是,当尝试连接到我的Postgresql时,遇到了麻烦。我用pgAdmin打开了Postgresql,连接的数据库中没有数据。我还删除了django应用程序中的所有条目,以防导入错误引起该错误。

$ docker-compose run web python manage.py migrate
Starting src_db_1 ... done
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, courses, products, sessions
Running migrations:
  Applying products.0001_initial...Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
psycopg2.DataError: NUMERIC precision 10000 must be between 1 and 1000
LINE 1: ...r(120) NOT NULL, "description" text NULL, "price" numeric(10...
                                                             ^

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  ... 
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
django.db.utils.DataError: NUMERIC precision 10000 must be between 1 and 1000
LINE 1: ...r(120) NOT NULL, "description" text NULL, "price" numeric(10...

这是我在models.py中创建价格DecimalField的地方。

from django.db import models
from django.urls import reverse

class Product(models.Model):
    title       = models.CharField(max_length=120) # max_length = required
    description = models.TextField(blank=True, null=True)
    price       = models.DecimalField(decimal_places=2, max_digits=100,blank=True)
    summary     = models.TextField(blank=True, null=True)
    featured    = models.BooleanField(default=False) # null=True, default=True

    def get_absolute_url(self):
        return reverse("products:product-detail", kwargs={"id": self.id}) #f"/products/{self.id}/"

这是我的manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "trydjango.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)
                                 ^

1 个答案:

答案 0 :(得分:0)

根据documentationmax_digits是允许的最大位数,而decimal_places是要存储的小数位数。

例如,如果max_digits = 6decimal_places = 2表示您将用4个数字表示小数位,用2个小数位数表示(最多6个数字)。允许我们最多存储9999,99。

话虽如此,我认为该错误与指定max_digits = 100有关。尝试使用较少的数字。