【问题标题】:heroku can't migrat database: cannot cast type time without time zone to timestamp with time zoneheroku 无法迁移数据库:无法将没有时区的类型时间转换为带时区的时间戳
【发布时间】:2021-09-06 10:41:42
【问题描述】:

我有一个包含 DateTimeField() 的简单表单的 Django 应用程序(在 Heroku 上运行) 该应用程序在本地运行良好,但是在我将其推送到 heroku 并执行 heroku run python3 migrate 后,它将返回错误:

psycopg2.errors.CannotCoerce:不能将没有时区的类型时间转换为有时区的时间戳 LINE 1: ..." TYPE timestamp with time zone USING "date_added"::timestam...

这是我的模型:

from django.db import models
from django.db.models.deletion import CASCADE
from django.db.models.fields.related import ForeignKey
from django.contrib.auth.models import User

# Create your models here.


class BlogPost(models.Model):
    title = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class BlogContent(models.Model):
    blogpost = ForeignKey(BlogPost, on_delete=models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(
        auto_now_add=True)  

    class Meta:
        verbose_name_plural = "blogcontents"

    def __str__(self):
        if len(self.text) > 50:
            return self.text[:50] + '...'
        else:
            return self.text

然后设置:

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

HTML:

{% extends 'blogs/base.html '%} {% block header %}
<h2>{{ title }}</h2>
{% endblock header %} {% block content %}
<p>contents:</p>
<p>
  <a href="{% url 'blogs:new_content' title.id %}">add new content</a>
</p>
<ul>
  {% for content in contents %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3>
        {{ content.date_added|date:'M d, Y H:i' }}
        <small>
          <a href="{% url 'blogs:edit_content' content.id  %}">edit content</a>
        </small>
      </h3>
    </div>
    <div class="panel-body">{{ content.text|linebreaks }}</div>
  </div>
  <!-- panel -->
  {% empty %}
  <li>There are no content for this title yet.</li>
  {% endfor %}
</ul>

{% endblock content %}

    {% extends "blogs/base.html" %} {% block header %}
<h1>Blog Titles</h1>
{% endblock header %} {% block content%}

<ul>
  {% for title in titles %}
  <li>
    <h3>
      {{ title.date_added|date:'M d, Y H:i' }}
      <a href="{% url 'blogs:contents' title.id %}">{{ title }}</a>
      <a href="{% url 'blogs:edit_title' title.id %}">edit title</a>
    </h3>
  </li>
  {% empty %}
  <li>No titles have been added.</li>
  {% endfor%}
</ul>

<h2>
  <li><a href="{% url 'blogs:new_title'%}">Add a new title</a></li>
</h2>

{% endblock content %}

日志:

(11_env) PS C:\Users\ZJ\Desktop\python\BlogPost> heroku run python3 manage.py migrate
 »   Warning: heroku update available from 7.53.0 to 7.54.1.
Running python3 manage.py migrate on ⬢ sheltered-plains-87656... up, run.8813 (Free)
Operations to perform:
  Apply all migrations: admin, auth, blogs, contenttypes, sessions
Running migrations:
  Applying blogs.0005_auto_20210620_0206...Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type time without time zone to timestamp with time zone
LINE 1: ..." TYPE timestamp with time zone USING "date_added"::timestam...

剩下的:

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

Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 244, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 594, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py", line 196, in _alter_field
    super()._alter_field(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 751, in _alter_field
    self.execute(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type time without time zone to timestamp with time zone
LINE 1: ..." TYPE timestamp with time zone USING "date_added"::timestam...

我尝试将 USE_TZ = True 更改为 USE_TZ = False ,从 html 文件中删除“ {{ title.date_added|date:'M d, YH:i' }} ”,重新创建 git 和应用程序,仍然会出现同样的错误。

当我在heroku上打开应用程序时,我创建一个帐户后,页面会返回服务器错误(500)。

请帮助我。谢谢。

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    我昨天遇到了这个问题。我知道我的回复有点晚了,你可能会在别处找到你的答案,但我会写信以防其他人在这里访问。

    您需要删除所有已推送的迁移文件,并让应用程序在那里通过 Heroku 生成新文件。

    为此,请使用命令heroku run shell 并使用rm 命令从您的“应用”/迁移中删除迁移文件(__init__.py 除外)。

    然后通过这个 shell 正常运行你的 python manage.py makemigrationspython manage.py migrate 并且这次你应该准备好了没有任何错误:)

    提示:您可以使用 .gitignore 忽略文件推送它们,这样您就不会再遇到任何迁移问题。例如,在我的 .gitignore 中,我添加了 **/migrations, *.key, db.sqlite, __pycache__, *.pyc, .idea ~&gt; (if you use PyCharm)。欲了解更多信息和有趣的点here

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2021-06-25
      • 2018-12-18
      • 1970-01-01
      • 2021-09-22
      • 2020-11-10
      • 2017-04-25
      • 1970-01-01
      • 2022-07-21
      相关资源
      最近更新 更多