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

时间:2015-02-02 02:24:26

标签: python django heroku deployment django-migrations

Python和Django新手在这里。我的部分代码基于Launch with Code的视频教程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曾经是保存用户的表格,但由于我不需要,我在本地工作时删除了它。

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

2 个答案:

答案 0 :(得分:3)

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

一切正常。

答案 1 :(得分:0)

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