Fixing Django – no such table exception

Django – no such table exception is an error which occurs in python when your program used to work with an existing database, and now you’re trying to start without a database. The error may also happen because of other reasons.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

Exploring the Django Error : no such table

Django no such table exception is an error which occurs sometimes when you start without a database, it can also happen for other reasons.

The error message should look like the message bellow. Make sure you are not dealing with another error.

                                                                       #
Exception Type: OperationalError at ...
Exception Value: no such table: ... # the name of your table
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

Solution 1 : edit your python file ” views.py “

First, we should understand that this error is pretty complex and occurs because of many reasons.

One of the most popular causes is trying to execute views.py which tries to access the database when imported.

The error takes place when your program used to work with a database, and now you’re trying to start without a database.

The solution is to edit the views.py file in order to avoid the error. We will explain the different line of code that you should add to your views file.

                                                                       #
from django.db.utils import OperationalError
format_list = [('', '(all)')]
geom_type_list = [('', '(all)')]
# this is the imports section of the code, we are importing from django.db.utils. we are importing
# geom_type_list and format_list. be careful with typos and the syntax
                                                                       #

After creating an import section for your code, we should add try to handle all the possible exceptions that might rise, please add these lines of code.

                                                                       #
try:
    format_list.extend([(x[0],x[0]) 
        for x in Format.objects.values_list('name')])
    geom_type_list.extend([(x[0],x[0]) 
        for x in Geom_type.objects.values_list('name')])
except OperationalError:
    pass  
                                                                       #

Now, you should be able to run your code without any errors, make sure to save the views file after editing it.

If the fix above helped you, consider supporting me on Kofi, thank you.

Solution 2 : Remove migration files and delete db.sqlite3

This solution is for windows and ubuntu users. The solution here is to Remove the migration files and delete db.sqlite3. Then, create and run the migrations again and synch your database.

First, let us delete /migrations/.pyc please adapt this command for windows. It should be easy

                                                                       #
find . -path "*/migrations/*.pyc"  -delete
                                                                       #

Do the same for /migrations/.py. Use the command here : find . -path “/migrations/.py” -not -name “init.py” -delete . Do not forget to adapt it for windows.

The next step is to delete the database, please backup the file first, just in case something goes wrong. The command : rm db.sqlite3 should be enough to delete the database.

Now, we should create the migrations and run them, run these two commands, start with this one.

                                                                       #
python manage.py makemigrations
                                                                       #

Followed by the command bellow

                                                                       #
python manage.py migrate
                                                                       #

Finally, do not forget to synch manage.py using the command manage.py migrate –run-syncdb

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up : 

The end, we arrived at the end of this article, I hope my article helped you solve the ” Django no such table exception ” error , I am sure there are at least 10 other reasons why this error occurs, I can not find them all and explain them, this is my best attempt guys,

Thank you for reading, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/