Opened 8 months ago
Closed 8 months ago
#36238 closed Uncategorized (invalid)
Migrate command executing project code cause migation failed.
| Reported by: | jcyang | Owned by: | |
|---|---|---|---|
| Component: | Uncategorized | Version: | 5.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I have an existing project with sqlite as database engine, and I want to recreate database file.
So I just deleted existing db file and migrations folder, and run command python manage.py migate.
I got the this error: django.db.utils.OperationalError: no such table: django_content_type.
It confused me for a long time, util I noticed the following among the output messages.
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\workflow\backend\project\urls.py", line 31, in <module>
path('api/', include('api.urls')),
^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\django\urls\conf.py", line 39, in include
urlconf_module = import_module(urlconf_module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\workflow\backend\api\urls.py", line 3, in <module>
from api import handlers
File "D:\workflow\backend\api\handlers.py", line 223, in <module>
class PermissionHandler(_CRUD):
File "D:\workflow\backend\api\handlers.py", line 233, in PermissionHandler
syspermission_type = ContentType.objects.get(app_label='common',model='syspermission')
Apparently, Django executed the sevice code in my project file, and that led to the error because no databases available now.
I commented out the configuration ROOT_URLCONF = 'project.urls' to make it not execute the code in my project. And then python manage.py migate worked very well. Then I restored the ROOT_URLCONF = 'project.urls' configuation item.
But that's just a bypass and weird way.
Hope Django team could fix that. Thanks.
Thanks for the report but ... Database code should not be run at module -level ... which appears to be what's happening here.
If you need assistance in using Django please feel free to ask help from a friendly community member on Discord or the Django forum 👍