I am having trouble running unit tests in my django app when it is running through Jenkins.
I have a CI/CD pipeline which runs python manage.py test
which is currently failing with the error:
django.db.utils.ProgrammingError: relation "auth_user" does not exist
.
Running tests locally works fine and the test database is normally created and the tests are ran normally.
I SSH-ed into the server where Jenkins is set up to try and manually run the tests but I get the same results, so I wouldnt' say it's anything Jenkins specific.
The postgres user has all privileges and CREATEDB on the database so postgres side seems to all be good.
This is the complete exception:
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
EDIT
I don't believe this question should be flagged as a duplicate. I had a very specific problem where the migrations were not entirely ran because of a missing migrations directory / __init__.py
file. This was particularly harder to debug since the tables come from a third party package that was installed.
I have resolved the issue. After @djvg mentioned models without migrations, I did a deeper dive. Turns out an application wasn't being automatically migrated and had to be have manual migrations ran for it.
I still have no idea why this stopped the tests completely, since that app has no dependencies on other apps and is not being used in test cases, but migrating that app specifically resolved my problem.