Django - no such table exception

雨燕双飞 提交于 2019-11-30 09:18:57

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 makemigrations
python 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 OperationalError
format_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

Adding to terry_brown's answer, this is what cause my problems. I had a custom user model with ForeignKey to another model. And I set defaults to first object in the DB. It worked when I had the data in the database. But when I started from scratch, it didn't work because it was executed immediately when imported (so not even migration went through).

class User(AbstractBaseUser, PermissionsMixin):
    subscription_plan = models.ForeignKey(SubscriptionPlan, default=SubscriptionPlan.objects.first().id)

I had to sacrifice that default (commenting it out).

UPDATE: A better solution would be to prepopulate your database with initial migration or fixtures.

Maybe out of time but...I have same problem when I tried to "clone" Django 1.11 installation into other directory and then with initial try to manage makemigrations.

I solve the problem following way:

  1. settup initial Django installation and creating main application by:

django-admin.py startproject app_name

  1. initial migrations manage makemigrations, manage migrate

  2. Setup the superuser:

manage createsuperuser

  1. copy all files and directories (Django applications) except the urls.py and settings.py in main directory

  2. Added all apps into INSTALLED_APPS

  3. manage makemigrations, manage migrate

  4. Copied settings.py and urls.py from source Django application directory

Thats it no errors and all is working fine.

Petr

If someone else has this problem and the accepted solution is not working, have a look at your db path, the db path should be absolute path, 'NAME': '/pathto-db/default.db',

Link

run below command. It solves me once this issue

manage.py migrate --run-syncdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!