PostgreSQL 11 到 16 django.db.utils.ProgrammingError

0 投票
1 回答
48 浏览
提问于 2025-04-13 19:08

我想把我的PostgreSQL版本从11升级到16。我在Dockerfile里改了版本号,当前是在本地,而且我还没有重要的数据,所以我删除了数据卷,然后在Django中尝试重新应用我的模型。到目前为止一切看起来都不错,但当我尝试运行makemigrations时,出现了这个错误:

django.db.utils.ProgrammingError: relation "account_lib_customers" does not exist
LINE 1: ..., "account_lib_customers"."profile" FROM "account_...

在这之前我做了什么


> I erase migrations folder of all app
> delete the volume in my docker app

account_lib是我公司的一个库,我需要在那儿做些什么吗?

这是完整的错误追踪信息

C:\Users\user\repli\web\django\venv\Scripts\python.exe C:\Users\user\repli\web\django\drf\manage.py runserver localhost:8000 
C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py:98: RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.
  warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py:98: RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.
  warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.UndefinedTable: relation "account_lib_customers" does not exist
LINE 1: ..., "account_lib_customers"."profile" FROM "account_...                         

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.12.1\Lib\threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.12.1\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\core\management\__init__.py", line 394, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\apps\config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\.pyenv\pyenv-win\versions\3.12.1\Lib\importlib\__init__.py", line 90, 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 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\user\repli\web\django\drf\contract\models\__init__.py", line 7, in <module>
    from .contracts import Contracts
  File "C:\Users\user\repli\web\django\drf\contract\models\contracts.py", line 8, in <module>
    from main.utils import utc_now, get_month_jst
  File "C:\Users\user\repli\web\django\drf\main\utils\__init__.py", line 18, in <module>
    from .specific_instance import
  File "C:\Users\user\repli\web\django\drf\main\utils\specific_instance.py", line 7, in <module>
    comp = Customers.objects.get(pk='8aefa7c0-e72e-4b78-83d8-34cdb3e3ab98')
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\models\query.py", line 645, in get
    num = len(clone)
          ^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\models\query.py", line 382, in __len__
    self._fetch_all()
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\models\query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\models\query.py", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\models\sql\compiler.py", line 1562, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py", line 122, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.ProgrammingError: relation "account_lib_customers" does not exist
LINE 1: ..., "account_lib_customers"."profile" FROM "account_...

1 个回答

1

我觉得这个警告说得很对:

C:\Users\user\repli\web\django\venv\Lib\site-packages\django\db\backends\utils.py:98: RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.

在某些应用程序中,apps.py::AppConfig.ready() 这个方法尝试访问 account_lib.Customers 这个模型,但它所依赖的数据库表还没有创建好。要解决这个问题,应该重新设计逻辑,避免在Django启动期间执行查询。

撰写回答