creyD
creyD

Reputation: 2126

Django Setup: No Such Table auth_user

Every time we set up our Django project in a new environment and try to initiate the server (migrate or runserver) the error message django.db.utils.OperationalError: no such table: auth_user shows (full error message below).

I am assuming this comes from using the user model as a foreign key in some of our models (example seen below) and as a new system is set up (no database created yet) Django tries to create the table for our custom model and fails because of the foreign key -the user table- as it wasn't created yet.

# Model for projects (example)
class Projekt(models.Model):
    project_name = models.CharField(max_length=100, unique=True)
    project_manager = models.ForeignKey(User, related_name="Manager", on_delete=models.DO_NOTHING)

My question is: is there any best practice on what to do to prevent this? For now, we are just copying the database from our running systems for every new developer we are getting, which works for now, but is not valid for open source distribution of the software.

I tried different stuff like this and this) already, but this didn't work out. I encountered this problem in each and every of my Django projects.


USERNAME$ python3 manage.py migrate
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 296, in execute
      return Database.Cursor.execute(self, query, params)
  sqlite3.OperationalError: no such table: auth_user

  The above exception was the direct cause of the following exception:

  Traceback (most recent call last):
    File "manage.py", line 15, in <module>
      execute_from_command_line(sys.argv)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
      utility.execute()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 350, in execute
      self.check()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
      include_deployment_checks=include_deployment_checks,
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 60, in _run_checks
      issues.extend(super()._run_checks(**kwargs))
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
      return checks.run_checks(**kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
      new_errors = check(app_configs=app_configs)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
      all_namespaces = _load_all_namespaces(resolver)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
      url_patterns = getattr(resolver, 'url_patterns', [])
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in url_patterns
      patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
      return import_module(self.urlconf_name)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
    File "<frozen importlib._bootstrap>", line 983, in _find_and_load
    File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 728, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/PROJECT_NAME/urls.py", line 8, in <module>
      path('i/', include('APP1.urls')), # Private space/ Development space
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
      urlconf_module = import_module(urlconf_module)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
    File "<frozen importlib._bootstrap>", line 983, in _find_and_load
    File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 728, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/urls.py", line 4, in <module>
      from . import views
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/views.py", line 11, in <module>
      from .forms import AddProject, AddTicket
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/forms.py", line 5, in <module>
      class AddProject(forms.Form):
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/forms.py", line 7, in AddProject
      project_manager = forms.ChoiceField(choices=[(user.id, user) for user in User.objects.all()])
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 268, in __iter__
      self._fetch_all()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 54, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
      cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
      return super().execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
      return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
      return executor(sql, params, many, context)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 296, in execute
      return Database.Cursor.execute(self, query, params)
  django.db.utils.OperationalError: no such table: auth_user

Upvotes: 1

Views: 4664

Answers (4)

ChrisE
ChrisE

Reputation: 576

I was using PyCharm and had the working directory set incorrectly in the debug config. Needs to point to the directory with manage.py in it. Probably not the issue for creyD, but might help others.

Upvotes: 0

Barrow
Barrow

Reputation: 35

I maybe late to answering this question but same happened to me what i did was, deleted pycache files from my app and related migration files and then also deleted the database file in my case sqlite file

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599926

You should add your migrations to version control and deploy them along with the rest of your code base.

If you have a lot of migrations which are now superfluous, you can squash them.

Upvotes: 1

skalet
skalet

Reputation: 384

Add migrations folder to git. This contains the information needed to create a new database correctly.

Upvotes: 3

Related Questions