RuntimeError:创建新内容类型时出错

Posted

技术标签:

【中文标题】RuntimeError:创建新内容类型时出错【英文标题】:RuntimeError: Error creating new content types 【发布时间】:2015-12-15 21:37:14 【问题描述】:
Operations to perform:
  Synchronize unmigrated apps: google, staticfiles, twitter, messages, reflect, allauth, facebook, rest_framework, crispy_forms
  Apply all migrations: account, django_comments, links, sessions, admin, fluent_comments, sites, auth, contenttypes, socialaccount
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Home\venv\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "C:\Users\Home\venv\lib\site-packages\django\core\management\__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Home\venv\lib\site-packages\django\core\management\base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Home\venv\lib\site-packages\django\core\management\base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Home\venv\lib\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 "C:\Users\Home\venv\lib\site-packages\django\core\management\sql.py", line 280, in emit_post_migrate_signal
    using=db)
  File "C:\Users\Home\venv\lib\site-packages\django\dispatch\dispatcher.py", line 201, in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Users\Home\venv\lib\site-packages\django\contrib\auth\management\__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(using).get_for_model(klass)
  File "C:\Users\Home\venv\lib\site-packages\django\contrib\contenttypes\models.py", line 78, in get_for_model
    "Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.

我尝试删除数据库和makemigrations,然后删除migrate。同样的错误

然后我在migrate 之前尝试了migrate contenttypes。回溯没有变化。

怎么了?

我正在使用 django==1.8

【问题讨论】:

试试python manage.py migrate contenttypes --fake ContentType.name 字段在 Django 1.8 中已被弃用。您可以尝试删除name 列的另一个选项,即。 ALTER TABLE django_content_type DROP COLUMN name. 回溯没有变化 检查这个Django ticket和Google groups thread关于这个问题。 这张票是 django==1.7 的。我从没用过 1.7 组并没有多大帮助。谢谢你,虽然@RahulGupta 【参考方案1】:

我刚刚遇到同样的错误信息。就我而言,是在我将其他字段注入 Django 组模型之后。也许你会做类似的事情。问题是为修改后的Group模型创建的auth迁移在Django的pip包文件夹中。

我在以下位置找到它:

<my_env_folder>/lib/python3.4/site-packages/django/contrib/auth/migrations/0007_auto_20151118_1635.py

您可以将其作为依赖项添加到失败的迁移中:

dependencies = [
    ('auth', '0007_auto_20151118_1635'),
    ('accounts', '0001_admin_user'),
]

但是从头开始重新构建环境时它会失败。所以这不是一个解决方案:-/。

【讨论】:

【参考方案2】:

在我的情况下,这与 https://code.djangoproject.com/ticket/25100 错误有关。由于某种原因,contenttypes.0002_remove_content_type_name 迁移未执行,投注标记为已执行。为了解决这个问题,我刚刚使用此查询手动删除了 name 字段:

alter table django_content_type drop column name;

之后,迁移开始工作。

【讨论】:

以上是关于RuntimeError:创建新内容类型时出错的主要内容,如果未能解决你的问题,请参考以下文章

Python Django 1.9 迁移错误“创建新内容类型时出错...”

Dlib“反序列化短类型对象时出错”

乘客:启动预加载器时出错

使用 pytorch 闪电进行多 GPU 训练时出错

(Python) Discord 机器人代码返回“RuntimeError:无法关闭正在运行的事件循环”

max 8 render to texture 出错