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

时间:2016-08-22 03:42:03

标签: python django django-guardian django-userena

安装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',    
]

6 个答案:

答案 0 :(得分:3)

问题在于这一行:

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)

违规部分是在User表创建之前使用的auth_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布尔值,您可以使用任何其他方式来避免makemigration运行代码,该代码取决于User.objects....结果

答案 1 :(得分:2)

  python manage.py migrate --run-syncdb  

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

答案 2 :(得分:1)

./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

的更多详情

愿它有所帮助

答案 3 :(得分:0)

我遇到了相同的错误,并且要修复该错误,我检查了所连接的数据库中是否存在上述错误表。不是。再次应用了迁移,它奏效了。

python manage.py migrate

python manage.py makemigrations

答案 4 :(得分:0)

运行:python3 manage.py迁移

答案 5 :(得分:0)

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

python manage.py makemigrations
python manage.py migrate

然后我就可以使用

创建一个超级用户
python manage.py createsuperuser

错误解决了!