2

So I have this app I wish to deploy to Heroku. I use django-heroku so it can run my postgres-sql stuff automatically. I added release: python mannge.py migrate to my Procfile. After the build, I get a deployment failed error, saying Release command failed. On opening the log, I have this error:

psycopg2.ProgrammingError: cannot cast type timestamp with time zone to integer
LINE 1: ...LUMN "established" TYPE integer USING "established"::integer
                                                          ^

My model with the established field is like this:

from django.db import models

# Create your models here.

class FederalMinistry(models.Model):
    full_name = models.CharField(max_length=400)
    short_name = models.CharField(max_length=20)
    description = models.TextField()
    established = models.PositiveIntegerField()
    # logo = models.ImageField(upload_to='fed_min_logos', blank=True, null=True, default='N/A')
    current_minister = models.CharField(max_length=500)
    permanent_secretary = models.CharField(max_length=500, default='N\A')
    headquarters = models.CharField(max_length=100, default='Abuja')
    twitter = models.URLField(blank=True, null=True, default='N/A')
    website = models.URLField(blank=True, null=True, default='N/A')

class Meta:
    verbose_name_plural = 'Federal Ministries'

def __str__(self):
    return self.full_name

Please why am I getting the error and his to fix it. I have no knowledge of postgres.

Here is the full traceback as was requested:

/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Operations to perform:
  Apply all migrations: admin, airports, auth, contenttypes, federal_ministries, sessions, states, universities
Running migrations:
  Applying federal_ministries.0002_auto_20190310_0129...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.ProgrammingError: cannot cast type timestamp with time zone to integer
LINE 1: ...LUMN "established" TYPE integer USING "established"::integer
                                                          ^
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 381, in execute_from_command_line
utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, 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 316, in run_from_argv
self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.6/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.6/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.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 122, in _alter_field
new_db_params, strict,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 663, in _alter_field
params,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.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
 Q 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.ProgrammingError: cannot cast type timestamp with time zone to integer
LINE 1: ...LUMN "established" TYPE integer USING "established"::integer

And here is the federal_ministries.0002_auto_20190310_0129 migration file:

    from django.db import migrations, models


    class Migration(migrations.Migration):

        dependencies = [
    ('federal_ministries', '0001_initial'),
]

        operations = [
    migrations.AlterModelOptions(
        name='federalministry',
        options={'verbose_name_plural': 'Federal Ministries'},
    ),
    migrations.AlterField(
        model_name='federalministry',
        name='established',
        field=models.PositiveIntegerField(),
    ),
]

And the dependency 0001_initial file:

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
]

    operations = [
    migrations.CreateModel(
        name='FederalMinistry',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('full_name', models.CharField(max_length=400)),
            ('short_name', models.CharField(max_length=20)),
            ('description', models.TextField()),
            ('established', models.DateTimeField()),
            ('current_minister', models.CharField(max_length=500)),
            ('permanent_secretary', models.CharField(default='N\\A', max_length=500)),
            ('headquarters', models.CharField(default='Abuja', max_length=100)),
            ('twitter', models.URLField(blank=True, default='N/A', null=True)),
            ('website', models.URLField(blank=True, default='N/A', null=True)),
        ],
    ),
]
Cole
  • 564
  • 1
  • 4
  • 14
  • change `established` to `DateTimeField` instead of `PositiveIntegerField` – suhailvs Mar 14 '19 at 08:32
  • Show the full trameback. Somewhere you are trying to set a datetime into that field. – Daniel Roseman Mar 14 '19 at 08:59
  • I just looked at the `established` fields in both the initial migration file and the 002 file. The first is a DateTime field while the second is a positive integer. So I think that's where the issue is from. Would it help to clear all migrations and destroy all databases using this [link](https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html)? – Cole Mar 14 '19 at 11:39

2 Answers2

2

I got it. I just needed to reset my migrations and delete the db.sqlite file, then ran migrations and migrate again. Fixed it!

Cole
  • 564
  • 1
  • 4
  • 14
0

If you are trying to cast a timestamp to PositiveIntegerField in PostgreSQL, you have to know that you can't.

Use a DateTimeField instead of a PositiveIntegerField for enstablished.

I think that this is related, take a look for further infos.

Manuel Fedele
  • 1,390
  • 13
  • 25
  • The established is meant to be in just a year format. Using datetimefield means I'll add a day and month too. Besides, why does PostgresSQL think I'm adding a time to it. The idea is that the values should be like 1980, or 1845. – Cole Mar 14 '19 at 08:36
  • Because he "sees" that you have a timezone in the value you're trying to insert in a `PositiveIntegerField`. – Manuel Fedele Mar 14 '19 at 08:39