35

我正在尝试从迁移Django 1.6Django 1.8. 我在 Django 1.6中South用于管理。migrations我已经成功地创建了新的迁移文件python manage.py makemigrations。运行时python manage.py migrate --fake-initial,我收到此错误

 Traceback (most recent call last):
  File "manage.py", line 39, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-    packages/django/core/management/__init__.py", line 338, in   execute_from_command_line
    utility.execute()
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-  packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 225, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
using=db)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
response = receiver(signal=self, sender=sender, **named)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(using).get_for_model(klass)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 78, in get_for_model
    "Error creating new content types. Please make sure contenttypes "

迁移文件之一0001_initial.py说:

dependencies = [
    ('auth', '0006_require_contenttypes_0002'),
    ('clients', '0002_auto_20150428_1551'),
    ('players', '0001_initial'),
]

我想这尤其是问题所在。有什么办法可以解决这个问题。任何帮助将不胜感激。

4

6 回答 6

48

据此,我认为这与“删除ContentType.name”有关。但不知何故它不起作用。

name通过从 'django_content_type' 表中手动删除列。例如。

'ALTER TABLE django_content_type DROP COLUMN name'

我能够应用迁移。也许这至少可以让你走得更远。

于 2015-05-05T18:10:04.580 回答
18

尝试先迁移身份验证应用程序,然后再迁移其他应用程序:

manage.py migrate auth
manage.py migrate <app_name>
于 2016-03-16T08:53:43.247 回答
7

就我而言,我为解决这个问题所做的就是更新到更新版本的 django。如果您使用 mac,请执行以下操作:

  1. pip install django --upgrade
  2. python manage.py makemigrations
  3. python manage.py 迁移
于 2015-09-05T23:27:06.493 回答
3

可能看起来很奇怪,但我通过升级到 Django 1.8 版解决了这个问题。最初我使用的是 1.7 版

于 2015-05-15T07:18:47.600 回答
2

要添加到 @int_ua 的评论,请将其作为依赖项添加到失败的迁移中:

dependencies = [
    ('contenttypes', '0002_remove_content_type_name'),
]

然后再次运行迁移。

于 2016-03-17T08:59:00.683 回答
2

我不得不在 Django 1.9.1 中合并两个系统,但我无法克服这个错误:

 "Error creating new content types. Please make sure contenttypes "

广泛的谷歌搜索和stackoverflowing是徒劳的。最后,我将调试行添加到

~/.virtualenvs/(venv_name)/lib/python2.7/site-packages/django/contrib/contenttypes/models.py

 except (OperationalError, ProgrammingError, IntegrityError):
        # It's possible to migrate a single app before contenttypes,
        # as it's not a required initial dependency (it's contrib!)
        # Have a nice error for this.
        print "\n\nError for Content type model "+opts.model_name+"\n\n"
        raise RuntimeError(
            "Error creating new content types. Please make sure contenttypes "
            "is migrated before trying to migrate apps individually."
        )

这告诉我导致错误并最终导致修复的模型名称。

我正在使用 Postgres,表 django_content_type 和 auth_permission 的序列号没有指向表的末尾,导致插入失败。

这 2 行已修复(基于此SO 帖子

SELECT pg_catalog.setval(pg_get_serial_sequence('django_content_type', 'id'), (SELECT MAX(id) FROM django_content_type)+1);
SELECT pg_catalog.setval(pg_get_serial_sequence('auth_permission', 'id'), (SELECT MAX(id) FROM auth_permission)+1);
于 2017-02-27T05:58:32.787 回答