python - 从脚本填充数据库时出现 Django Heroku DataError

标签 python django postgresql sqlite heroku

我制作了一个脚本来读取 xml 文件并从中填充数据库。当我在本地运行它时,它没有问题。但是当我在 heroku 上运行它时,它会遍历并填充一些数据(恰好 6 个对象),但随后会抛出此错误:

skripta_vnos.py 是我在 shell 中运行的填充脚本

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/app/skripta_vnos.py", line 97, in <module>
    dob.save()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 922, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 961, in _do_insert
    using=using, raw=raw)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 1063, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
DataError: value too long for type character varying(100)

我的模型是这样的:

class Supply(models.Model):
    name = models.CharField(max_length=255) 
    maticna = models.CharField(max_length=255)
    organ = models.CharField(max_length=255, null=True, blank=True)
    pos = models.CharField(max_length=255, null=True, blank=True)
    city = models.CharField(max_length=255, null=True, blank=True)
    postn= models.CharField(max_length=255, null=True, blank=True)
    hisna = models.CharField(max_length=255, null=True, blank=True)

    email = models.CharField(max_length=255, null=True, blank=True)

    def __str__(self):
        return self.name.encode("utf-8")

    def get_absolute_url(self):
        return reverse('supply-detail', args=[str(self.id)])

最佳答案

您收到此错误是因为您的字符串之一大于最大长度。 如果它在本地工作,您可能更改了最大长度并且没有在 Heroku 上迁移您的更改。

关于python - 从脚本填充数据库时出现 Django Heroku DataError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45709913/

相关文章:

python - 如何计算字符串中的某些单词(不仅仅是一个单词),然后如果单词数量不同则输出不同的代码?

python - 将 PyMySQL 与 MariaDB 结合使用

python - Django admin 很难定制吗?

Django 和 Tastypie : accessing an object related to a newly created resource

postgresql - 如何在PostgreSQL的where子句中写rownum

sql - PostgreSQL 更新未按预期工作

python - 创建排列矩阵

python - 张量 Tensor ("predictions/Softmax:0", shape=(?, 1000), dtype=float32) 不是该图的元素

python - Daphne,安装 django channel 时出现扭曲的 pip 错误

performance - 性能 postgresql 9.1.9/9.3.6 与 9.4.1