【问题标题】:DataError: numeric field overflowDataError:数值字段溢出
【发布时间】:2020-10-16 20:58:50
【问题描述】:

DETAIL:精度为 2、比例为 2 的字段必须舍入为小于 1 的绝对值。

我正在尝试将 Django 应用程序部署到 Heroku。我的 Django 应用程序使用 Sqlite3,它在我的系统上运行良好。如果您在产品上单击“喜欢”,它会将评级参数更新 1。

这是 Product 模型的 sn-p

class Product(models.Model):

 image = models.CharField( max_length= 500, blank = True, help_text= 'Image link' )
 title = models.CharField( max_length= 200)
 description = models.TextField(blank = True)
 price = models.DecimalField(max_digits= 10, decimal_places= 2, blank = True, )
 ratings = models.DecimalField(max_digits=10, decimal_places=2, blank= True, default = 0)

Before clicking 'like' the ratings on the product is 4

After clicking 'like' the ratings increases to 5 without any error Result

这就是我更新评分的方式:

if request.method == 'POST':
      obj = Product.objects.get(id = i)
      obj.ratings += Decimal(1)
      obj.save()

但是,在部署到 Heroku 之后,当我点击“喜欢”时,我会收到此错误。

/home/1/ 处的数据错误 数值字段溢出

DETAIL:精度为 2、比例为 2 的字段必须舍入为小于 1 的绝对值。

(/home/1/ 是路线)

我不明白为什么

精度 2,比例 2?

我知道 heroku 可能正在使用 PostgreSQL,正如它在错误页面上所说的那样

Error page Database info

但我的 max_digit 是 10,小数位是 2。之前我也是用 max_digits=1000 开始的。还是有数据错误。如何解决此错误?

【问题讨论】:

  • 另外,如果它有帮助,当我在 bash 上运行迁移时,我会收到此错误 - django.db.utils.DataError: NUMERIC precision 10000 must be between 1 and 1000

标签: django sqlite django-models heroku-postgres


【解决方案1】:

已解决

所以当我在 heroku bash 上运行 migrate 命令时,我注意到这里发生了错误

Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying products.0002_auto_20200626_0112...Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InvalidParameterValue: NUMERIC precision 10000 must be between 1 and 1000

这一行:

正在应用 products.0002_auto_20200626_0112...Traceback(最近一次调用最后一次):

所以我去了我的迁移文件夹并检查了 0002_auto_20200626_0112 迁移。原来我已将十进制字段中的 max_length 设置为 10000。即使我之前更改并应用了迁移,该文件的存在也导致了错误。所以我自己手动将该文件上的值更改为值 1 并且错误已解决。

【讨论】:

    猜你喜欢
    • 2018-02-09
    • 2023-03-12
    • 2011-11-12
    • 2013-07-05
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多