N
N
Nekr0z2017-04-18 16:48:24
Django
Nekr0z, 2017-04-18 16:48:24

Django ORM. Why does OperationalError: no such table appear when creating migrations?

When executed manage.py makemigrations, an error appears stating that the table (to which it is necessary to migrate) does not exist:

Call List
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "D:\enviropments\NewElion\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "D:\enviropments\NewElion\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\enviropments\NewElion\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\enviropments\NewElion\lib\site-packages\django\core\management\base.py", line 342, in execute
    self.check()
  File "D:\enviropments\NewElion\lib\site-packages\django\core\management\base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\enviropments\NewElion\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\enviropments\NewElion\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\enviropments\NewElion\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "D:\enviropments\NewElion\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "D:\enviropments\NewElion\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\enviropments\NewElion\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\enviropments\NewElion\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\enviropments\NewElion\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "D:\Python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\YandexDisk\WORK!\Projects Django\test\Elion\Elion\urls.py", line 5, in <module>
    from about import views as about_views
  File "D:\YandexDisk\WORK!\Projects Django\test\Elion\about\views.py", line 6, in <module>
    from .forms import ContactMessageForm, SubmitApplication
  File "D:\YandexDisk\WORK!\Projects Django\test\Elion\about\forms.py", line 42, in <module>
    class SubmitApplication(forms.Form):
  File "D:\YandexDisk\WORK!\Projects Django\test\Elion\about\forms.py", line 43, in SubmitApplication
    regions = [(obj.id, obj.region) for obj in AreasWork.objects.all()]
  File "D:\enviropments\NewElion\lib\site-packages\django\db\models\query.py", line 256, in __iter__
    self._fetch_all()
  File "D:\enviropments\NewElion\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "D:\enviropments\NewElion\lib\site-packages\django\db\models\query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "D:\enviropments\NewElion\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "D:\enviropments\NewElion\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:\enviropments\NewElion\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\enviropments\NewElion\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:\enviropments\NewElion\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\enviropments\NewElion\lib\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: AreasWork
This list of calls shows that forms.pythis application is called in the place where there is a call to this table:
# Elion\about\forms.py:
class SubmitApplication(forms.Form):
    regions = [(obj.id, obj.region) for obj in AreasWork.objects.all()] # Ошибка в этой строке
    choice_region = [(None, 'Выберите город')] + regions + [('0', 'Другой')]
    region = forms.ChoiceField(label='Область', choices=choice_region,
                               widget=forms.Select(attrs={'class': 'form-control'}))
    ............

If at this point to remove the call to the database, then no more errors appear. Even after executing manage.py migrateand forms.pyreturning the code back, everything will work fine, without errors.
Also, I found that someone had a similar problem and also an error in the file forms.pywhen accessing the database.
It is clear that to solve the problem, you need to stupidly remove the call to the database in the form, but I wonder why this happens at all and what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neoliz, 2017-05-26
@Nekr0z

Because with a simple python manage.py makemigrations, the networking environment is completely raised. And since forms.py is imported somewhere, then janga sees it at startup, but does not see the table in the database. That's why the error.
We must first write models, then migrations, then everything else. Or Until the migration is done, do not access the database. You form a query to the database with your AreasWork.objects.all(), that's all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question