Skip to content Skip to sidebar Skip to footer

Django.db.utils.operationalerror: No Such Table: Auth_user

After I install Django-userena,there is an error my django version :1.9.5 I just install django-userena step by step ,but when i migrate it ,an error happend and I don't how to sol

Solution 1:

./manage.py migrate

if your Django version is 1.9 or lower, use

./manage.py syncdb

then

python manage.py createsuperuser

more details on https://github.com/django/django/blob/master/django/contrib/auth/forms.py

May it help

Solution 2:

The problem is this line:

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

guardian\management\__init__.py is calling User before it is defined.

Why is it called before it is defined? ...well, it is called by makemigrations to find out if there are changes in the db to define them (the changes that are found)

...so this is a "chicken and egg" problem.

I have the same error with a similar problem but the offending code was my own code (not a library like guardian)

My code was like this:

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

The offending part is the User being used before the auth_user table is created

I solved by only executing the code when the table is present. This happens when there is no OperationError. And you can know it with a try/except like this:

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 = Noneif created:
    CHAT_BOT_USER.set_password(settings.EMAIL_HOST_PASSWORD)
    CHAT_BOT_USER.save()

This way makemigrations runs the except code and runserver runs the try code.

Note the created boolean, you may use any other way to avoid makemigration run code that depends on the User.objects.... results

Solution 3:

I was get same error and to fix the same, I checked if the mentioned table in error is present in connected DB or not. It was not. Applied migration again and it worked.

python manage.py migrate

python manage.py makemigrations

Solution 4:

  python manage.py migrate --run-syncdb

This command will look into which tables were not created and will create all the needed tables.

Solution 5:

RUN: python3 manage.py migrate

Post a Comment for "Django.db.utils.operationalerror: No Such Table: Auth_user"