fable
fable

Reputation: 51

Django 1.7 Migration Errors when deploying on Heroku

Python and Django newbie here. Part of my code is based on the video tutorial Launch with Code from Coding for Entrepreneurs.

Using Python 2.7.5 with Django 1.7.4 and trying to deploy my app on Heroku, I get stuck with an error I don't understand when synchronizing the database. I run:

heroku run python manage.py migrate

And I get the following error:

Running `python manage.py migrate` attached to terminal... up, run.9158
    Operations to perform:
      Apply all migrations: auth, sessions, admin, ppm, contenttypes
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying ppm.0001_initial... OK
      Applying ppm.0002_auto_20150131_1822... OK
      Applying ppm.0003_auto_20150131_2241... OK
      Applying ppm.0004_auto_20150131_2246... OK
      Applying ppm.0005_auto_20150131_2317... OK
      Applying ppm.0006_auto_20150201_0008... OK
      Applying ppm.0007_auto_20150201_0020... OK
      Applying ppm.0008_auto_20150201_0023... OK
      Applying ppm.0009_auto_20150201_0031... OK
      Applying ppm.0010_auto_20150201_0032... OK
      Applying ppm.0011_auto_20150201_0035... OK
      Applying ppm.0012_auto_20150201_0132... OK
      Applying ppm.0013_auto_20150201_0219... OK
      Applying ppm.0014_auto_20150201_0247...Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
        output = self.handle(*args, **options)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
        executor.migrate(targets, plan, fake=options.get("fake", False))
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
        self.apply_migration(migration, fake=fake)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
        migration.apply(project_state, schema_editor)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
        operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
        schema_editor.alter_field(from_model, from_field, to_field)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 454, in alter_field
        self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 600, in _alter_field
        params,
      File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 102, in execute
        cursor.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)
    django.db.utils.ProgrammingError: column "username_id" cannot be cast automatically to type integer
    HINT:  Specify a USING expression to perform the conversion.

I can't make sense of this error as I don't have any username_id in my ppm app models.

models.py:

from django.db import models
from django.contrib.auth.models import User
from django.utils.encoding import smart_unicode

class SpielInput(models.Model):

    user = models.ForeignKey(User)
    spiel = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return smart_unicode(self.user.username)

    def game(self):
        return smart_unicode(self.spiel)

class AnamneseInput(models.Model):

    user = models.ForeignKey(User)
    anamnese = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return smart_unicode(self.user.username)

    def amn(self):
        return smart_unicode(self.anamnese)

class TherapieInput(models.Model):

    user = models.ForeignKey(User)
    therapie = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return smart_unicode(self.user.username)

    def ther(self):
        return smart_unicode(self.therapie)

I have tried to destroy my Heroku app many times and changing my models.py but nothing worked. I guess I should make use of the HINT: Specify a USING expression to perform the conversion. but I have no idea how.

Edit 1: add file that triggered the error

0014_auto_20150201_0247.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


    class Migration(migrations.Migration):

        dependencies = [
            ('ppm', '0013_auto_20150201_0219'),
        ]

        operations = [
            migrations.AlterField(
                model_name='spielinput',
                name='username',
                field=models.ForeignKey(to='ppm.ProjectUser'),
                preserve_default=True,
            ),
        ]

The problem seems to be with field=models.ForeignKey(to='ppm.ProjectUser'),. ProjectUser used to be the table to save the users, but as I didn't need it, I deleted it when still working locally.

Any help would be great! Thanks in advance

Upvotes: 1

Views: 1434

Answers (2)

Jessamyn Smith
Jessamyn Smith

Reputation: 1649

The recommended Django upgrade path is to delete your south migrations: https://docs.djangoproject.com/en/1.7/topics/migrations/#upgrading-from-south

Upvotes: 0

fable
fable

Reputation: 51

The problem was that I'm using Django 1.7 (with migrations included) whereas the tutorial I followed use Django 1.6 (using South to do migrations).

When working locally, I have changed my models.py many times and thus the database has changed many times as well, with every changes saved into my folder ppm/migrations.

So all I needed to do was to add all those migration change files into my .gitignore file. Thus, they are not taken into the production app on Heroku.

.gitignore:

ppm/migrations/0001_initial.py
[etc.]
ppm/migrations/0014_auto_20150201_0247.py
[etc.]

then run:

git add .
git commit -m "Modified .gitignore"
git rm --cached ppm/migrations/00*
git commit -m "Removed migration files"
git push heroku app master
heroku run manage.py migrate

And everything works fine.

Upvotes: 4

Related Questions