I am trying to upgrade a django application from 4.2 (LTS) to 5.2.4 (LTS) but have run into an issue with one of the management commands “initial_migrate” that was working fine with 4.2 but now seems to try and query the (non existant) tables in the database and explodes.
The command code (filename is initial_migrate) is:
from django.core.management.base import no_translations
from django.core.management.commands.migrate import Command as MigrateCommand
class Command(MigrateCommand):
# The system checks import urls.py, which imports lots of other modules
# which attempt to access the database. This will prevent the database from
# being initialized. To work around that, disable the system checks on the
# initial migration.
@no_translations
def handle(self, *args, **options):
options['skip_checks'] = True
super(Command, self).handle(*args, **options)
The traceback I get is:
Traceback (most recent call last):
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
psycopg2.errors.UndefinedTable: relation "redirects_redirect" does not exist
LINE 1: ...create_dt", "redirects_redirect"."update_dt" FROM "redirects...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/srv/www/tendenci16/manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
~~~~~~~~~~~~~~~^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/management/base.py", line 416, in run_from_argv
self.execute(*args, **cmd_options)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/management/base.py", line 457, in execute
self.check(**check_kwargs)
~~~~~~~~~~^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/management/base.py", line 492, in check
all_issues = checks.run_checks(
app_configs=app_configs,
...<2 lines>...
databases=databases,
)
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/checks/registry.py", line 89, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/checks/urls.py", line 44, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/core/checks/urls.py", line 63, in _load_all_namespaces
url_patterns = getattr(resolver, "url_patterns", [])
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/utils/functional.py", line 47, in __get__
res = instance.__dict__[self.name] = self.func(instance)
~~~~~~~~~^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/urls/resolvers.py", line 718, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/utils/functional.py", line 47, in __get__
res = instance.__dict__[self.name] = self.func(instance)
~~~~~~~~~^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/urls/resolvers.py", line 711, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib64/python3.13/importlib/__init__.py", line 88, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 1026, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/srv/www/tendenci16/conf/urls.py", line 6, in <module>
from tendenci.urls import handler500 # noqa: F401
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/tendenci/urls.py", line 76, in <module>
re_path(r'^redirects/', include('tendenci.apps.redirects.urls')),
~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/urls/conf.py", line 39, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib64/python3.13/importlib/__init__.py", line 88, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/tendenci/apps/redirects/urls.py", line 2, in <module>
from . import views
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/tendenci/apps/redirects/views.py", line 16, in <module>
from tendenci.apps.redirects import dynamic_urls
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/tendenci/apps/redirects/dynamic_urls.py", line 3, in <module>
urlpatterns = get_redirect_patterns()
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/tendenci/apps/redirects/utils.py", line 13, in get_redirect_patterns
for redirect in redirects:
^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/models/query.py", line 384, in __iter__
self._fetch_all()
~~~~~~~~~~~~~~~^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/models/query.py", line 1949, in _fetch_all
self._result_cache = list(self._iterable_class(self))
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/models/query.py", line 91, in __iter__
results = compiler.execute_sql(
chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size
)
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/models/sql/compiler.py", line 1623, in execute_sql
cursor.execute(sql, params)
~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 122, in execute
return super().execute(sql, params)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 79, in execute
return self._execute_with_wrappers(
~~~~~~~~~~~~~~~~~~~~~~~~~~~^
sql, params, many=False, executor=self._execute
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 100, in _execute
with self.db.wrap_database_errors:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/srv/dev/tendenci16/.venv/lib64/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
django.db.utils.ProgrammingError: relation "redirects_redirect" does not exist
LINE 1: ...create_dt", "redirects_redirect"."update_dt" FROM "redirects...
^
I have tried to follow using a debugger, but I get lost in a lot of argument parsing magic, but what I do see is that once the execution hits django/core/management/base.py execute() options[“skip_checks”] is false. Running “python manage.py migrate --skip-ckecks” works fine from the command line.
I don’t know if the change here is related or not: Fixed #35920 -- Observed requires_system_checks in migrate and runser… · django/django@2ce4545 · GitHub
What is the right way to fix this module for django 5.2.4 ?