Home > Back-end >  Django test error: django.db.utils.ProgrammingError: relation "auth_group" does not exist
Django test error: django.db.utils.ProgrammingError: relation "auth_group" does not exist

Time:12-21

I have a django app which has not been tested lately. I have to run a custom command. I have written a test for it and run all migrations. I was able to successfully run all migrations. When I tried to run the test then the following error was thrown.

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

I tried to run migrate auth seperately. There were no migrations to apply

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

CodePudding user response:

The following django-app help to run django tests without affecting the migration conflicts.

Install 'django-test-without-migrations'

pip install django-test-without-migrations

add it it in INSTALLED_APPS

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

Then run,

python manage.py test --nomigrations

--nomigrations make sure your tests won't fail due to the migrations issues.

Refer: https://pypi.org/project/django-test-without-migrations/

CodePudding user response:

python manage.py makemigrations auth

then

python manage.py makemigrations
  • Related