gpt4 book ai didi

python - Django `auth` 和 `contenttypes` 中断 syncdb

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:57 26 4
gpt4 key购买 nike

当我发出命令时:

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

最佳答案

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

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

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

关于python - Django `auth` 和 `contenttypes` 中断 syncdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11210122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com