Django - no such table exception Django - no such table exception python python

Django - no such table exception


I solved the same problem with these steps :

  • Delete your database (db.sqlite3 in my case) in your project directory
  • Remove everything from __pycache__ folder under your project subdirectory
  • For the application you are trying to fix, go to the folder and clear migrations and __pycache__ directories

When you are sure you have cleared all the above files, run:

python manage.py makemigrationspython manage.py migrate

I hope this helps.


Another case wihch can generate the no such table error. If your views.py or similar executes code that tries to access the DB when imported, i.e. importing views.py has side effects, then starting from scratch won't work.

This happens when your code was working with an existing DB, and now you're trying to start without a DB. Just change views.py so it can be imported without side effects. If you don't want to fix the design, do something like:

from django.db.utils import OperationalErrorformat_list = [('', '(all)')]geom_type_list = [('', '(all)')]try:    format_list.extend([(i[0],i[0])         for i in Format.objects.values_list('name')])    geom_type_list.extend([(i[0],i[0])         for i in Geom_type.objects.values_list('name')])except OperationalError:    pass  # happens when db doesn't exist yet, views.py should be          # importable without this side effect


run below command. It solves me once this issue

manage.py migrate --run-syncdb