当我发出命令时:
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的示例,除了我在MY模型上有自定义属性,以便它知道它们默认的数据库。在syncdb
我发表评论settings.py INSTALLED_APPS
和django.contrib.auth
时django.contrib.contenttypes
有效。有这个问题已经有一段时间了,但是直到现在我还需要开始认证。如果你想要我的db路由器,我也会发布
答案 0 :(得分:3)
我在这里解释了类似的问题: django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_' for key 'content_type_id'")
您无需注释contrib.auth和contrib.contenttypes。只需确保所有django模型 - 用户,会话,权限仅用于1个数据库,这可以被视为主数据库。
这不会直接解决您的问题,但可以作为处理多个db和db路由器的起点。您需要知道的是每个模型在数据库中都有它的内容类型。 当django对象 - 用户/会话/权限不限于单个数据库 - 然后将它们创建到每个数据库中时,会出现问题。由于内容类型使模型具有唯一性,因此在多个数据库中使用单一类型的内容类型可能会导致上述其他SO问题中解释的问题。
答案 1 :(得分:0)
在Django 1.4中,您必须在项目的顶级文件夹中启动此命令(path_to_project / you_project NOT path_to_project / you_project / you_project)