OperationalError:删除数据库和迁移后没有这样的表

时间:2017-03-03 08:18:58

标签: django django-models

我删除了db以及用于重置数据库的所有迁移。但删除后,我试图运行 Makemigrations它显示错误

  

OperationalError:没有这样的表:student_app_grade

我有一个名为student_app的应用 并且有一个名为Grade

的模型

这是完整的错误

  

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
  

这是我的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)

4 个答案:

答案 0 :(得分:3)

尝试,

  1. 重建迁移:python manage.py makemigrations student_app
  2. migrate:python manage.py migrate
  3. 希望它有所帮助。

答案 1 :(得分:0)

检查student_app/migrations/__init__.py中是否有拼写错误(init之前和之后是否有两个下划线?文件名中是否有空格????

运行makemigrations时是否出现错误?

删除您的迁移和数据库并粘贴到问题 makemigrations完整输出完整追溯,如果错误出现在其他地方(并在出现时解释:您可能过早导入某些内容)

更新:问题出在forms.py(AddtionalInformationForm)的第59行。在Python中,构造函数代码应该在def __init__(self, ...)中。请参阅Django的表单文档,并了解有关python的面向对象编程的更多信息。另外:在Django中,模板用于呈现HTML。

答案 2 :(得分:0)

我修复了与登录应用程序相同的问题。

(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

答案 3 :(得分:0)

我知道这篇文章很旧,但是如果其他任何人遇到了这个问题,则可能是由于您在允许迁移之前运行了某个程序。例如在view.py或可能正在更新等中

由于数据库已被重置,因此错误听起来像是正在尝试对它执行某些操作,即使它正在重置中(因此不存在)。