Django测试迁移失败:relation 'auth_group'不存在
问题描述
我有一个许久未测试的Django应用,需运行自定义命令,已编写测试用例并执行完所有迁移,生产环境下应用可正常运行。但执行python manage.py test时抛出错误:django.db.utils.ProgrammingError: relation "auth_group" does not exist,单独执行migrate auth显示无迁移可应用。
错误详情
python manage.py test Creating test database for alias 'default'... Got an error creating the test database: database "test_ILIDD_db" already exists Type 'yes' if you would like to try deleting the test database 'test_ILIDD_db', or 'no' to cancel: yes Destroying old test database for alias 'default'... Traceback (most recent call last): File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\utils.py", line 86, in _execute return self.cursor.execute(sql, params) psycopg2.errors.UndefinedTable: relation "auth_group" does not exist The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 39, in <module> execute_from_command_line(sys.argv) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line utility.execute() File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\__init__.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv super().run_from_argv(argv) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv self.execute(*args, **cmd_options) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\base.py", line 369, in execute output = self.handle(*args, **options) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\test_without_migrations\management\commands\_base.py", line 78, in handle super(CommandMixin, self).handle(*test_labels, **options) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\commands\test.py", line 53, in handle failures = test_runner.run_tests(test_labels) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\test\runner.py", line 684, in run_tests old_config = self.setup_databases(aliases=databases) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\test\runner.py", line 604, in setup_databases return _setup_databases( File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\test\utils.py", line 169, in setup_databases connection.creation.create_test_db( File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\base\creation.py", line 67, in create_test_db call_command( File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\__init__.py", line 168, in call_command return command.execute(*args, **defaults) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\base.py", line 369, in execute output = self.handle(*args, **options) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\commands\migrate.py", line 202, in handle self.sync_apps(connection, executor.loader.unmigrated_apps) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\core\management\commands\migrate.py", line 340, in sync_apps self.stdout.write(" Running deferred SQL...\n") File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\base\schema.py", line 115, in __exit__ self.execute(sql) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute cursor.execute(sql, params) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Anaconda3\envs\nlp-ilidd-processor\lib\site-packages\django\db\backends\utils.py", line 86, in _execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: relation "auth_group" does not exist
单独迁移auth的结果
Operations to perform: Apply all migrations: auth Running migrations: No migrations to apply.
解决方案
- 排查
test_without_migrations库影响:从错误栈可见测试流程调用了该库,它会跳过迁移直接同步模型,可能导致auth模块表未正确创建。可临时移除该库,用默认测试命令:python manage.py test --keepdb(--keepdb保留测试数据库避免重复创建);若必须使用该库,需确保它能正确处理auth依赖,或手动在测试前创建auth相关表。 - 手动初始化测试数据库auth表:
# 创建测试数据库(若不存在) createdb test_ILIDD_db # 执行auth模块迁移 python manage.py migrate auth --database=default # 执行所有迁移 python manage.py migrate --database=default # 运行测试 python manage.py test - 彻底清理测试数据库:
# 删除测试数据库 dropdb test_ILIDD_db # 重新运行测试,让Django自动创建并迁移数据库 python manage.py test - 检查
INSTALLED_APPS配置:确认项目settings.py中INSTALLED_APPS包含django.contrib.auth,缺失会导致auth模块迁移和表创建被跳过。 - 检查auth迁移文件完整性:若旧项目丢失auth初始迁移文件,可备份现有迁移后重新生成:
python manage.py makemigrations auth python manage.py migrate auth
内容的提问来源于stack exchange,提问作者arun.subhash-IBS
相关产品推荐
相关产品推荐

