4

当我发出命令时:

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. 这个问题有一段时间了,但一直推迟到现在,当我需要开始身份验证时。如果你想要我的数据库路由器,我也会发布

4

2 回答 2

3

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

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

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

于 2012-06-26T15:16:53.330 回答
0

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

于 2012-09-14T10:29:18.830 回答