繁体   English   中英

django.db.utils.OperationalError:没有这样的表:auth_user

[英]django.db.utils.OperationalError: no such table: auth_user

在我安装 Django-userena 后,我的 django 版本有一个错误:1.9.5 我只是一步一步地安装 django-userena,但是当我迁移它时,发生了一个错误,我不知道如何解决它。

    Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    emit_post_migrate_signal(self.verbosity, self.interactive, connection.alias)
  File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 50, in emit_post_migrate_signal
    using=db)
  File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Python27\lib\site-packages\guardian\management\__init__.py", line 33, in create_anonymous_user
    User.objects.get(**lookup)
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 381, in get
    num = len(clone)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 240, in __len__
    self._fetch_all()
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_user

应用:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'userena',
    'guardian',
    'easy_thumbnails',
    'accounts',    
]
./manage.py migrate

如果您的 Django 版本是 1.9 或更低,请使用

./manage.py syncdb

那么

python manage.py createsuperuser

有关https://github.com/django/django/blob/master/django/contrib/auth/forms.py 的更多详细信息

可能会有所帮助

问题是这一行:

File "C:\\Python27\\lib\\site-packages\\guardian\\management\\__init__.py", line 33, in create_anonymous_user User.objects.get(**lookup)

guardian\\management\\__init__.py在定义之前调用User

为什么在定义之前调用它? ......好吧,它是由被称为makemigrations以找出是否有变化db来定义它们(被发现的变化)

...所以这是一个“鸡和蛋”的问题。

我有类似问题的相同错误,但有问题的代码是我自己的代码(不是像guardian那样的库)

我的代码是这样的:

CHAT_BOT_USER, created = User.objects.get_or_create(username='rosty', \\ email=settings.EMAIL_HOST_USER)

有问题的部分是在创建auth_user表之前使用的User

我通过仅在表存在时执行代码来解决。 当没有OperationError时会发生这种情况。 你可以通过try/except来了解它:

from django.db import OperationalError

try:
    CHAT_BOT_USER, created = User.objects.get_or_create(username='rosty', email=settings.EMAIL_HOST_USER)

except OperationalError:
    CHAT_BOT_USER = None

if created:
    CHAT_BOT_USER.set_password(settings.EMAIL_HOST_PASSWORD)
    CHAT_BOT_USER.save()

通过这种方式, makemigrations运行except代码,而runserver运行try代码。

请注意created布尔值,您可以使用任何其他方式来避免依赖于User.objects....结果的makemigration运行代码

我遇到了同样的错误并修复了同样的错误,我检查了所提到的错误表是否存在于连接的数据库中。 事实并非如此。 再次应用迁移,它奏效了。

python manage.py migrate

python manage.py makemigrations
  python manage.py migrate --run-syncdb  

此命令将查看未创建哪些表,并创建所有需要的表。

运行:python3 manage.py migrate

我在使用聊天室时遇到了同样的错误。 我使用删除db.sqlite3文件并删除应用程序迁移文件夹中的0001_initial.py文件来解决,然后我执行了以下语句:-

python manage.py makemigrations
python manage.py migrate

然后我能够使用创建一个超级用户

python manage.py createsuperuser

错误解决了!

只要没有创建表,就会发生这种情况。

所以使用命令进行迁移: python manage.py makemigrations迁移后你需要迁移它: python manage.py migrate

如果它不起作用,则使用:

python manage.py makemigrations yourappname

python manage.py migrate yourappname

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM