Django测试错误,在Django测试期间迁移失败;django.db.utils.ProgrammingError:关系“auth_group”不存在

hts6caw3  于 2023-01-31  发布在  Go
关注(0)|答案(3)|浏览(111)

我有一个django应用程序,最近还没有测试过。我必须运行一个自定义命令。我已经为它写了一个测试,并运行了所有的迁移。
我能够成功运行所有迁移。我能够运行命令,我的应用程序工作正常。
但是当我试着运行django测试时,抛出了下面的错误。

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

我尝试单独运行migrate auth。没有要应用的迁移

Operations to perform:
  Apply all migrations: auth
Running migrations:
  No migrations to apply.
06odsfpq

06odsfpq1#

下面的django-app帮助运行django测试而不影响迁移冲突。
安装'django-test-without-migrations'

pip install django-test-without-migrations

将其添加到INSTALLED_APPS

INSTALLED_APPS = (
    # ...
    'test_without_migrations',
)

那就跑吧

python manage.py test --nomigrations

--nomigrations确保您的测试不会因为迁移问题而失败。
参考:https://pypi.org/project/django-test-without-migrations/

bzzcjhmw

bzzcjhmw2#

python manage.py makemigrations auth

那么

python manage.py makemigrations
plicqrtu

plicqrtu3#

就像我在问题中说的,我可以成功地运行迁移,也可以运行我的django应用程序,但是当我试图运行测试时,出现了上面提到的错误。
我通过编辑settings.py或(local_settings.py)镜像我的数据库解决了这个问题,现在我可以运行测试,没有任何迁移问题。

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'USER': 'mydatabaseuser',
    'NAME': 'mydatabase',
    'TEST': {
        'MIRROR': 'default',
    },
},

}

相关问题