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

Posted

技术标签:

【中文标题】在 Heroku 上部署时出现 Django 1.7 迁移错误【英文标题】:Django 1.7 Migration Errors when deploying on Heroku 【发布时间】:2015-03-31 23:03:45 【问题描述】:

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

将 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曾经是保存用户的表,但我不需要,所以在本地工作的时候删除了。

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

【问题讨论】:

您能否发布触发错误的迁移文件? 对不起,你到底是什么意思? ppm/migrations文件夹中,会有一个名为0014_auto_20150201_0247的文件;使用此文件的内容更新您的问题。 【参考方案1】:

问题是我使用的是 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

一切正常。

【讨论】:

【参考方案2】:

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

【讨论】:

我用 Django 1.7 完成了所有工作,所以我不需要从 South 升级。我不明白的是我已经在 Heroku 服务器上推送(即git push)我的本地迁移文件,这就是触发错误的原因。因此,我修改 .gitignore 文件的答案。

以上是关于在 Heroku 上部署时出现 Django 1.7 迁移错误的主要内容,如果未能解决你的问题,请参考以下文章

PySFTP失败,“在部署Django / Heroku时出现”找不到主机X的主机密钥”

在heroku上部署django网站时出错

如何解决在 Heroku 上部署 React 应用程序时出现的错误

在 Heroku 上部署 MEAN 应用程序时出现应用程序错误

在heroku上部署应用程序时出现过时的纱线锁文件错误

在 Heroku 上部署 React 应用程序和 Nginx 时出现问题