How to Resolve Django: OperationalError Associated with Admin Component

How to Resolve Django: OperationalError Associated with Admin Component

Django - No such table: main.auth_user__old

Django remains one of the best tightly coupled frameworks of Python. By tightly coupled, it means Django enforces you to always create web apps the right way, and in the long run, makes you an efficient software engineer or developer. Read More About Benefits of Django Here.

There are several benefits that developing web apps with Django offer, one of such is It's easier to build better Web apps more quickly and with less code using Django. Such associated flavours for instance is the default admin app component shipped alongside every Django app by default. This admin can be setup pretty easy with only 2 lines of codes:

from .models import AppComponentName
admin.site.register(AppComponentName)

This helps you or your clients (whom you're developing for) manage products or services faster and more efficiently and for better CMS experience. The bottom line is, less coding time is spent on overall development time.

However, Django admin 'app' doesn't come without a few nightmares :-) One of such errors is OperationalErrors. That is Django - No such table: main.auth_user__old

OperationalError.PNG

This error occurs whenever there is a write/add operation in the admin database. For instance, while adding products to the Product app component (for a CMS app), Creating a new user from the admin panel, or generally populating to a database table via the admin app component.

This tutorial will walk you through resolving this error.

Step 1: Upgrade your Django Version

The issue is known with Django versions earlier than 2.1.5

So first thing first, upgrade your Django version to the most stable versions from 2.1.5.

To upgrade your Django version, from your terminal or virtual environment, >>>

For Version 3.1.2

pip install django==3.1.2

Read more about various releases and versions of Django and raised issues from the official Django website

Step 2: Stop the Django Webserver

It is assumed that you are getting this error from the runtime, i.e while your Django server is running from the Django root (manage.py). First thing first, stop your server via your terminal with

CTRL-BREAK 
OR
CTRL + C

StopServer.PNG

Step 3: Delete the db.sqlite3 file

There's an SQLite database file that contains all models' tables of your entire web project. This contains also your migrations from your models and replicates all models as tables. Delete the file after you'd stopped the server.

Click on the file from your IDE (using pycharm in this guide. Refer to appropriate path in your own IDE), and delete.

Sqlite.PNG

Step 4: Make Your DB Migrations Afresh

Now that you've deleted, make a new migration to a new db.sqlite file that will be created at runtime.

 python(python3 for Mac) manage.py makemigrations
 python(python3 for Mac) manage.py migrate

This will migrate all models to respective tables in the newly created SQLite database.

Step 5: Recreate Your Super Admin User

Remember we are now working with a new database, hence, the need to recreate the old users again. In any case, we only need to create the superuser admin from the terminal. Do this using>>>

creatsuperuser.png

python(python3 for Mac) manage.py createsuperuser. Enter your desired username and press enter.
Username: admin. You will then be prompted for your desired email address:
Email address: admin@example.com. ...
Password: ********** Password (again): ********* Superuser created successfully

Step 6: Start Your Web Server Again

Now is the time to test if this error has been handled. From your terminal again, run your server

python(python3 for Mac) manage.py runserver

This should resolve the issue.

Summary

In resolving the issue with writing or adding to admin app component, here are the major highlights again:

  • Go to the virtual environment and install django not less than 2.1.5
    *pip install django==2.1.7
  • Delete the db.sqlite3 file in your root folder.
  • Create the new db.sqlite3 in your root folder using Re-run migrations: python3 manage.py makemigrations python3 manage.py migrate

Leave comments in the comments section as I would love to know if you find this resource helpful in any way.