【问题标题】:django heroku postgres db does not detect current migration django.db.utils.DataError: value too longdjango heroku postgres db 未检测到当前迁移 django.db.utils.DataError: value too long
【发布时间】:2018-09-01 16:36:00
【问题描述】:

当我运行我的脚本来填充我的 Django postgres DB 时,我得到 django.db.utils.DataError: value too long for type character varying(30)。为了克服这个错误,我更改了我的代码以获取最大长度为 200 个字符的 URL。

from django.db import models
from django.contrib.postgres.fields import ArrayField

class ArticleScheme(models.Model):
    title = models.CharField(max_length=200)
    newsoutlet = models.CharField(max_length=10)
    url = models.CharField(max_length=200)
    date_joined = models.DateField()
    text = models.CharField(max_length=100)
    polarity = models.CharField(max_length=4)
    subjectivity = models.CharField(max_length=4)
    keywords = ArrayField(models.CharField(max_length=10), blank=True)

    def __str__(self):
        return ("title:{}\nnewsoutet:{}\nurl:{}\ndate_joined:{}"
        + "\ntext:{}\npolarity:{}\nsubjectivity:{}\nkeywords:{}").format(
          self.title, self.newsoutlet, self.url, self.date_joined, self.text,
          self.polarity, self.subjectivity, str(self.keywords)
        )

我使用命令 python3 manage.py makemigrations appname 保存了文件。然后我将该代码推送到 Heroku。使用 Heroku 运行 bash,迁移位于文件夹中,但 python3 manage.py migrate appname 未检测到要更新的迁移。 我运行我的脚本来填充 Heroku 上的数据库,我收到以下错误。

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.DataError: value too long for type character varying(30)


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 "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/app/HerokuNewsApp/management/commands/NewsScraper.py", line 133, in handle
    self.FindAndWriteArticle(options['url'], options["news"])
  File "/app/HerokuNewsApp/management/commands/NewsScraper.py", line 120, in FindAndWriteArticle
    self.AddArticleToDB(article, blob.sentiment, newsoutlet)
  File "/app/HerokuNewsApp/management/commands/NewsScraper.py", line 46, in AddArticleToDB
    ArticlePost.save()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1280, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(30)

显然,Django 未能检测到我的数据库的更新以进行迁移。我尝试手动删除迁移并再次使用 makemigration 命令,但没有成功。我还尝试了 python manage.py makemigration --fake appname zero。但是,我相信这也会导致错误。

有什么建议吗?

【问题讨论】:

  • 我也尝试过使用 TruncatingCharField 作为此处描述的 stackoverflow.com/questions/3459843/…,但我得到了相同的 django.db.utils.DataError: value too long for type character varying(30)。

标签: django database python-3.x postgresql heroku


【解决方案1】:

我在 Heroku 上重置了数据库,之后代码就可以工作了。如果您查看 Postgres 上的文档,它会说添加列需要完全重写。我认为问题在于添加长度更长的新条目也需要重写。

https://docs.djangoproject.com/en/2.0/topics/migrations/#postgresql

幸运的是,我没有任何重要数据,但是如果您需要数据,请在重置数据库之前制作数据库条目的 JSON 文件。然后,您将能够重新加载数据。

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 1970-01-01
    • 2020-04-07
    • 2021-09-09
    • 2018-01-27
    • 1970-01-01
    • 2014-07-21
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多