[Django] #36238: Migrate command executing project code cause migation failed.

5 views
Skip to first unread message

Django

unread,
Mar 9, 2025, 7:41:20 AM3/9/25
to django-...@googlegroups.com
#36238: Migrate command executing project code cause migation failed.
------------------------+-----------------------------------------
Reporter: jcyang | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------+-----------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/36238>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 9, 2025, 8:05:07 AM3/9/25
to django-...@googlegroups.com
#36238: Migrate command executing project code cause migation failed.
-------------------------------+--------------------------------------
Reporter: jcyang | Owner: (none)
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by David Sanders):

* resolution: => invalid
* status: new => closed

Comment:

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 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/36238#comment:1>
Reply all
Reply to author
Forward
0 new messages