【问题标题】:Django `auth` and `contenttypes` breaks syncdbDjango `auth` 和 `contenttypes` 破坏了同步数据库
【发布时间】:2012-06-27 22:27:10
【问题描述】:

当我发出命令时:

python manage.py syncdb --database=mydb

它显示输出如下...

Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Traceback (most recent call last):
File "manage.py", line 14, in <module>
    execute_manager(settings)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 459,     in execute_manager
    utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 382,     in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 196, in     run_from_argv
    self.execute(*args, **options.__dict__)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 232, in     execute
    output = self.handle(*args, **options)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 371, in     handle
    return self.handle_noargs(**options)
File "/usr/lib/python2.7/site-packages/django/core/management/commands/syncdb.py",     line 110, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
File "/usr/lib/python2.7/site-packages/django/core/management/sql.py", line 189, in     emit_post_sync_signal
    interactive=interactive, db=db)
File "/usr/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in     send
    response = receiver(signal=self, sender=sender, **named)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py",     line 35, in create_permissions
    ctype = ContentType.objects.get_for_model(klass)
File "/usr/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line     42, in get_for_model
    defaults = {'name': smart_unicode(opts.verbose_name_raw)},
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 134, in     get_or_create
    return self.get_query_set().get_or_create(**kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 442, in     get_or_create
    return self.get(**lookup), False
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 361, in get
    num = len(clone)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 85, in     __len__
    self._result_cache = list(self.iterator())
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 291, in     iterator
    for row in compiler.results_iter():
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 763,     in results_iter
    for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line     818, in     execute_sql
    cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in     execute
    return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line     337, in     execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: no such table: django_content_type

我有一个自定义数据库路由器,它基本上设置为 django 的示例,除了我的模型上有一个自定义属性,以便它知道它们默认使用哪个数据库。 syncdb 在我的 settings.py INSTALLED_APPS 中有效,我注释掉:django.contrib.authdjango.contrib.contenttypes。这个问题有一段时间了,但一直推迟到现在,当我需要开始身份验证时。如果你想要我的数据库路由器,我也会发布

【问题讨论】:

    标签: python django django-models python-2.7


    【解决方案1】:

    我在这里解释了一个类似的问题: django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_' for key 'content_type_id'")

    您不需要注释掉 contrib.auth 和 contrib.contenttypes。只需确保所有 django 模型 - 用户、会话、权限仅在 1 个数据库中使用,可以视为主数据库。

    这不会直接解决您的问题,但可以作为处理多个 db 和 db 路由器的起点。您需要知道的是每个模型在数据库中都有它的内容类型。 当 django 对象 - 用户/会话/权限不限于单个数据库 - 然后它们被创建到每个数据库中时,就会出现问题。并且由于内容类型使模型独一无二,因此在多个数据库中具有单一类型的内容类型可能会导致上述其他 SO 问题中解释的问题。

    【讨论】:

      【解决方案2】:

      在 Django 1.4 中,您必须在项目的顶级文件夹中启动此命令(path_to_project/you_project NOT path_to_project/you_project/you_project)

      【讨论】:

      • 只有一个地方可以调用syncdb,因为它基于manage.py文件的位置
      • 其实你可以在内部文件夹中运行syncdb。这可能是错误的,但你可以。如果您使用 slqlite 它会创建新数据库;我不知道这是否是 django 1.4 的错误,但在我的项目中它运行时没有任何问题。
      猜你喜欢
      • 2021-11-30
      • 2011-08-01
      • 2014-10-23
      • 2022-08-05
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多