1

I deleted db and all migrations for resetting my database. But after the deletion when I tried to run Makemigrations it is showing the error that

OperationalError: no such table : student_app_grade

I have an app named student_app And in that there is a model with name Grade

this is the full error

python3 manage.py makemigrations

Traceback (most recent call last):
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: student_app_grade

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/management/base.py", line 342, in execute
    self.check()
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/home/ajit/.local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ajit/Documents/Choice_feedback/Choice_feedback_main/Choice_feedback_main/urls.py", line 8, in <module>
    url(r'^user/', include('registration_app.urls')),
  File "/home/ajit/.local/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ajit/Documents/Choice_feedback/Choice_feedback_main/registration_app/urls.py", line 10, in <module>
    from registration_app.views import (
  File "/home/ajit/Documents/Choice_feedback/Choice_feedback_main/registration_app/views.py", line 11, in <module>
    from registration_app.forms import (
  File "/home/ajit/Documents/Choice_feedback/Choice_feedback_main/registration_app/forms.py", line 45, in <module>
    class AddtionalInformationForm(forms.Form):
  File "/home/ajit/Documents/Choice_feedback/Choice_feedback_main/registration_app/forms.py", line 59, in AddtionalInformationForm
    for grade in q:
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/models/query.py", line 256, in __iter__
    self._fetch_all()
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/models/query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: student_app_grade
xlB

this is my forms.py

    import roman
from collections import OrderedDict
from django.contrib.auth.forms import (
        UserCreationForm,
        UserChangeForm,
        PasswordChangeForm
)
from django import forms
from django.contrib.auth.models import User
from student_app.models import Grade


class RegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)
    username = forms.CharField(max_length=24, widget=forms.TextInput(attrs={'name':"username"}))

    class Meta:
        model = User
        fields = (
            'username',
            'email',
            'password1',
            'password2'
        )
    def save(self,commit = True):
        user = super(RegistrationForm,self).save(commit = False)
        # user.first_name = self.cleaned_data['first_name']
        # user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']

        if commit:
            user.save()
        return user


class EditProfileForm(UserChangeForm):

    class Meta:
        model = User
        fields = (
            'email',
            'password'
        )

class AddtionalInformationForm(forms.Form):
    hash_class_ordered = []
    hash_div_ordered = []
    hash_class = []
    hash_div = []
    grade_ol = OrderedDict()
    grade_ = []
    grade_n = []
    yq = []
    div_ = []
    div_n = []
    grade_t = ()

    q = Grade.objects.all()
    for grade in q:
        x = grade.grade
        temp = x.split("-")
        hash_class.append(temp[0])
        hash_div.append(temp[1])

    for c in hash_class:
        if c not in grade_n:
            grade_n.append(c)
    for d in hash_div:
        temp = (d, d)
        if temp not in div_:
            div_.append(temp)
            div_.sort()

    for h_c in grade_n:
        h_c_n = roman.fromRoman(h_c)
        temp = (h_c, h_c_n)
        hash_class_ordered.append(temp)
        hash_class_ordered.sort()

    for c in hash_class_ordered:
        c = (c[0], c[0])
        yq.append(c)

    grade = tuple(yq)
    div = tuple(div_)
    first_name = forms.CharField(max_length=20)
    last_name = forms.CharField(max_length=20)
    student_class_grade = forms.ChoiceField(choices=grade)
    student_class_div = forms.ChoiceField(choices=div)
    roll_no = forms.CharField(max_length=10)
Ajit
  • 11
  • 5
  • "I deleted db and all migrations..." so you _have_ also removed your migration files? – Bono Mar 03 '17 at 10:38
  • I removed all my migration files except __init__ from all my apps. – Ajit Mar 03 '17 at 11:09
  • Please show the full traceback. – knbk Mar 03 '17 at 11:45
  • Trace back means !? – Ajit Mar 03 '17 at 11:46
  • The traceback is the part of the error that says which functions are currently being called, and in which files. It says "Traceback (most recent call last):" at the top. – knbk Mar 03 '17 at 11:50
  • @knbk ,Traceback (most recent call last): File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "/home/ajit/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table: student_app_grade – Ajit Mar 03 '17 at 11:54

5 Answers5

5

Try,

  1. rebuild migrations: python manage.py makemigrations student_app
  2. migrate: python manage.py migrate

Hope it helps.

1

I know this post is old but in case anyone else runs into this, the problem could possibly be if you are having a program run before the migrations are being allowed to take place. Such as in the view.py or maybe updating, etc.

Since the db is being reset, the error sounds like something is trying to be done to it even though it was in the middle of being reset (thus not existing).

  • Yes in my case, I was importing tables in forms. So every time, you run `python manage.py makemigrations` or `python manage.py runserver`. django recheck the forms, hence it cannot find the table in db. Hence giving not found error. In that case, what I did is delete/comments out the lines which using django ORM query in forms. Hence re-run migrations. Now its work and I revert the changes in form usiing git. – Pratik Sharma Oct 21 '21 at 14:35
0

Check that you don't have typos in student_app/migrations/__init__.py (Do you have two underlines before and after init?? Is there a space somewhere in the filename????)

Does the error appear when running makemigrations?

Delete your migrations and db and paste into your question the full output of makemigrations or the full traceback if the error appears somewhere else (and explain when it appears: you might be importing something too early)

Update: The problem is in line 59 in forms.py (AddtionalInformationForm). In Python constructor code should be in def __init__(self, ...). Please refer to Django's forms docs and learn more about python's object oriented programming. Also: in Django, templates are used to render HTML.

Udi
  • 29,222
  • 9
  • 96
  • 129
  • `student_app/migrations/__init__.py` is empty – Ajit Mar 04 '17 at 01:10
  • after the error shown my PyCharm stopped showing different colours for class ,def all ! Also the imports from other models is showing as an error. – Ajit Mar 04 '17 at 04:41
  • Could you please little more specific like what I need to do ! Actually I didn't understand what you just said ! – Ajit Mar 04 '17 at 07:33
  • You need to learn how to use Django's forms API. Go and learn. – Udi Mar 04 '17 at 07:34
  • Ajitjoseph@icloud.com is my email Id. Could you please share your email Id with me ! If possible please send a mail to me so that I can consult you while learning about forms ! Please – Ajit Mar 04 '17 at 07:39
  • Also one more doubt , why this arise only when I deleted my db and tried a new migration ? The project was working well before deleting! – Ajit Mar 04 '17 at 07:48
  • Because it accessed the DB while the project is loading. – Udi Mar 04 '17 at 08:00
0

I fixed the same issue as following for my Login app.

(venv) MacBook-Pro:src admin$ python manage.py makemigrations Login
Migrations for 'Login':
  Login/migrations/0001_initial.py
    - Create model User
(venv) MacBook-Pro:src admin$ python manage.py migrate Login
Operations to perform:
  Apply all migrations: Login
Running migrations:
  Applying Login.0001_initial... OK
(venv) MacBook-Pro:src admin$ python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
July 11, 2017 - 13:05:56
Django version 1.11.2, using settings 'CabProj.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C
hygull
  • 8,464
  • 2
  • 43
  • 52
0

See the full error traceback. In that, you can find the line in file where you are querying from the db, maybe mostly ( in forms ). You need to temporary delete/comments out the those lines. And then re-run the migrations. You need to keep tracing back and keep deleting/comments outing the lines having query after makemigrations command. At last it will create the migrations files.

Pratik Sharma
  • 165
  • 2
  • 9