Home > OS >  Getting ProgrammingError: relation "auth_user" does not exist while running test
Getting ProgrammingError: relation "auth_user" does not exist while running test

Time:05-05

Lately I faced this problem that is driving me crazy and didn't find any solution on the internet. I don't no when the problem created or what did I do that caused this but now every time I want to run my test in my Django app this errors show up:

Traceback (most recent call last):
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/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/mohammad/Projects/IP_Camera/manage.py", line 22, in <module>
    main()
  File "/home/mohammad/Projects/IP_Camera/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/commands/test.py", line 68, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/test/runner.py", line 1000, in run_tests
    old_config = self.setup_databases(
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/test/runner.py", line 898, in setup_databases
    return _setup_databases(
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/test/utils.py", line 220, in setup_databases
    connection.creation.create_test_db(
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/backends/base/creation.py", line 79, in create_test_db
    call_command(
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 198, in call_command
    return command.execute(*args, **defaults)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/base.py", line 98, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 255, in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 402, in sync_apps
    with connection.schema_editor() as editor:
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 157, in __exit__
    self.execute(sql)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 192, in execute
    cursor.execute(sql, params)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/mohammad/Projects/IP_Camera/venv/lib/python3.10/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

Does anyone have any idea what is going on here? Thanks in advance.

Answer

The answer checked. I commented further information and details.

CodePudding user response:

See this question.

This appears to happen with any inconsistency between your models and migration files when running unit tests.

Make sure you have migrations/__init__.py in your app and migrate.

  • Related