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.I'm able to run the command and my app works as excepted.
But when I tried to run the django 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.
Like I said in my question I was able to run the migrations successfully and was able to run my django app as well. But when I tried to run the tests the above mentioned error occurred.
I solved the issue by mirroring my db by editing settings.py
or (local_settings.py
).
Now I am able to run the test without any migration issues.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'USER': 'mydatabaseuser',
'NAME': 'mydatabase',
'TEST': {
'MIRROR': 'default',
},
},
}