在 Heroku 上部署时 Django 1.7 迁移错误

Django 1.7 Migration Errors when deploying on Heroku

Python 和 Django 新手在这里。我的部分代码基于视频教程 Launch with Code from Coding for Entrepreneurs.

使用 Python 2.7.5 和 Django 1.7.4 并尝试在 Heroku 上部署我的应用程序,我在同步数据库时遇到了一个我不明白的错误。 我运行:

heroku run python manage.py migrate

我收到以下错误:

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.

我无法理解这个错误,因为我的 ppm 应用程序模型中没有任何 username_id

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)

我曾多次尝试破坏我的 Heroku 应用程序并更改我的 models.py 但没有任何效果。我想我应该使用 HINT: Specify a USING expression to perform the conversion. 但我不知道如何使用。

编辑 1:添加触发错误的文件

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,
            ),
        ]

问题似乎出在 field=models.ForeignKey(to='ppm.ProjectUser'), 上。 ProjectUser 曾经是 table 来拯救用户,但因为我不需要它,所以我还在本地工作时删除了它。

任何帮助都会很棒!提前致谢

问题是我使用的是 Django 1.7(包括迁移),而我遵循的教程使用的是 Django 1.6(使用 South 进行迁移)。

在本地工作时,我更改了我的 models.py 多次,因此数据库也更改了很多次,每次更改都保存到我的文件夹 ppm/migrations 中。

所以我需要做的就是将所有这些迁移更改文件添加到我的 .gitignore 文件中。因此,它们不会被纳入 Heroku 上的生产应用程序。

.gitignore:

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

然后 运行:

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

一切正常。

推荐的Django升级路径是删除你的南迁: https://docs.djangoproject.com/en/1.7/topics/migrations/#upgrading-from-south