Django单元测试报错:relation 'auth_user'不存在求助
问题
在Jenkins运行Django应用单元测试时执行python manage.py test失败,报错:
django.db.utils.ProgrammingError: relation "auth_user" does not exist
本地测试正常,SSH到Jenkins服务器手动运行同样报错,排除Jenkins本身问题。Postgres用户拥有数据库全部权限及CREATEDB权限,数据库层面无异常。完整异常栈如下:
Traceback (most recent call last): File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) psycopg2.errors.UndefinedTable: relation "auth_user" does not exist The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/agent/jenkins/workspace/INP-Backend/manage.py", line 22, in <module> main() File "/home/agent/jenkins/workspace/INP-Backend/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv super().run_from_argv(argv) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/test.py", line 68, in handle failures = test_runner.run_tests(test_labels) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/test/runner.py", line 1045, in run_tests old_config = self.setup_databases( File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/test/runner.py", line 941, in setup_databases return _setup_databases( File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/test/utils.py", line 220, in setup_databases connection.creation.create_test_db( File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 78, in create_test_db call_command( File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 198, in call_command return command.execute(*args, **defaults) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 96, in wrapped res = handle_func(*args, **kwargs) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 314, in handle self.sync_apps(connection, executor.loader.unmigrated_apps) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 480, in sync_apps self.stdout.write(" Running deferred SQL...") File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 164, in __exit__ self.execute(sql) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 199, in execute cursor.execute(sql, params) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 103, in execute return super().execute(sql, params) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute return self._execute_with_wrappers( File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: relation "auth_user" does not exist
补充说明:此问题为特定场景——因缺失migrations目录或__init__.py文件导致迁移未完全执行,且涉及第三方包的表,排查难度较大。
解决方法
检查并修复应用的migrations目录结构
- 确认每个Django应用下存在
migrations目录,且目录内有__init__.py文件(即使文件为空)。如果缺失,手动创建:mkdir your_app/migrations touch your_app/migrations/__init__.py - 检查第三方依赖包的迁移是否被正确加载。若项目使用自定义用户模型或修改了auth相关配置,需确保对应的迁移已生成并能被Django识别。
- 确认每个Django应用下存在
重新生成并执行迁移
- 在Jenkins服务器上(或本地测试后同步到代码库),清除旧的迁移文件(保留
__init__.py):find . -path "*/migrations/*.py" -not -name "__init__.py" -delete find . -path "*/migrations/*.pyc" -delete - 重新生成迁移文件:
python manage.py makemigrations - 执行迁移:
python manage.py migrate - 再次运行测试:
python manage.py test
- 在Jenkins服务器上(或本地测试后同步到代码库),清除旧的迁移文件(保留
验证测试数据库的迁移流程
手动触发测试数据库的创建和迁移,排查是否存在迁移执行不全的问题:python manage.py create_test_db --keepdb python manage.py migrate --database=test确认
auth_user表存在后,再运行测试。
内容的提问来源于stack exchange,提问作者4bs3nt
相关产品推荐
相关产品推荐

