0

I've got a Django app (running on Heroku ) with a simple form that contains a DateTimeField() the app work well locally, but after I push it to the heroku, and do heroku run python3 migrate, it will return an error :

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...

here is my models:

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

then the settings:

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 %}

logs:

(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 rest:

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...

I've try to change the USE_TZ = True to USE_TZ = False ,delete the " {{ title.date_added|date:'M d, Y H:i' }} " from the html files, recreat the git and app, still get the same error.

When I open the app on heroku,after I create an account, the page will return Server Error (500).

Please help me. Thanks.

torek
  • 448,244
  • 59
  • 642
  • 775
Pygamer
  • 1
  • 1

1 Answers1

0

I met that issue yesterday. I know my reply comes a little late and you may found your answer elsewhere, but I'll write in case someone else visit this here.

You need to delete all the migration files you have pushed, and let the applications make its new files through Heroku there.

To do that use the command heroku run shell and remove the migrations file (except __init__.py) from your 'app'/migrations with rm command.

Then through this shell run normally your python manage.py makemigrations and python manage.py migrate and you should be ready to go without any error this time :)

tip: You can ignore files from pushing them with .gitignore so you won't have any issue again with migrations. For example, in my .gitignore I added **/migrations, *.key, db.sqlite, __pycache__, *.pyc, .idea ~> (if you use PyCharm). For more info and interesting points here.

Te0SX
  • 1
  • 1