1

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.
4
  • You will need to include some details, specifically your code. Commented May 3, 2022 at 7:36
  • What do you need? Commented May 3, 2022 at 7:47
  • It's difficult to point out what is the exact problem without information as to how you've set up your code. Where is this auth_user defined? What is your model like? etc.. Commented May 3, 2022 at 7:50
  • It is the built in django user model from django.contrib.auth.models Commented May 5, 2022 at 12:37

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it. Thanks a lot for helping. The problem was when I re-cloned the project the migration files were not in my repository and practically they didn't exist. What I did was created a raw database and created the migrations again and change the database back to my primary database and everything was working perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.