1

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
creyD
  • 1,972
  • 3
  • 26
  • 55
  • There is nothing wrong with committing `migrations` folder to your github repository. Have you defined your custom User model in your `settings.py` file as `AUTH_USER_MODEL=MyUser` ? You should add important part of your settings file related to this question so that we could verify everything. And Where the `User` model in your `Project` model is imported from? – SK. Fazlee Rabby Oct 21 '18 at 13:04
  • Thanks for your reply. We actually don't use a custom User model, just the user model as a foreign key for some models, so the settings probably don't really help. I think that this would be the same for other foreign key relationships too... – creyD Oct 21 '18 at 13:22
  • @SK.FazleeRabby Is there any (official) way to recreate the migrations without the huge development overhead? Like objects and attributes we improved or removed during development, which we don't want to recreate just to delete them a few migrations later... – creyD Oct 21 '18 at 13:52
  • You can also bisect your commits to see where this error was introduced. You can read more at https://git-scm.com/docs/git-bisect. – skalet Oct 25 '18 at 15:28
  • @skalet Sadly this error occurs only on new systems, when no database is initiated yet, but on all the running system there is no problem. So this is not the solution, I'm sorry. – creyD Oct 25 '18 at 22:23

4 Answers4

3

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

skalet
  • 365
  • 1
  • 9
  • Thank you for your answer. Shouldn't it be possible to set up a new database every time? We are changing a lot on our models, we have a lot of migrations just for test... (plus there would be a huge amount of tables to create (...) just to delete them a few migrations later)... – creyD Oct 21 '18 at 12:38
  • If not sensitive, can your provide the complete (or as much as you can) output when you run `manage.py migrate` on a freshly cloned repo? Also, add `__init__.py` to your git, this is what makes your folders into python packages. – skalet Oct 22 '18 at 08:23
  • Thank you, added the __init__.py to our git (didn't test with it yet) and posted the whole error code in the question (Edit 2). Thank you so much for your ongoing help. – creyD Oct 22 '18 at 10:03
  • Can you also include the normal output, before the error is raised? I want to see the migration messages from django. – skalet Oct 22 '18 at 11:34
  • Included the output in an updated version of my question. – creyD Oct 30 '18 at 12:52
  • This actually didn't resolve it for me, but as I reworked the models, I learned that I made a few mistakes back then. So this should help most people, therefore I am accepting it as the answer. – creyD May 16 '19 at 14:24
1

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you for your reply! I did squash the migrations and pushed not only the squashed but also the original migrations and it still shows the same error... – creyD Oct 21 '18 at 20:26
0

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

Barrow
  • 29
  • 6
0

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.

ChrisE
  • 376
  • 4
  • 14