2

I am pulling my hair out. I just can't get migrations to work anymore. Every time I run python3 manage.py makemigrations or python3 manage.py makemigrations app_name I get the following error:

    Traceback (most recent call last):
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: catalog_fault

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 "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 361, in execute
    self.check()
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 584, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/peter/Documents/21q/locallibrary/urls.py", line 60, in <module>
    loader.load_db()
  File "/home/peter/Documents/21q/catalog/loader.py", line 106, in load_db
    add_game(*game, batch=True)
  File "/home/peter/Documents/21q/catalog/models.py", line 315, in add_game
    fault = add_fault(fault_name, batch=batch)
  File "/home/peter/Documents/21q/catalog/models.py", line 327, in add_fault
    fault = Fault.objects.get(fault__icontains = fault_name)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 402, in get
    num = len(clone)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 256, in __len__
    self._fetch_all()
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1133, in execute_sql
    cursor.execute(sql, params)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: catalog_fault

I have read lots of Q&As on Stackoverflow and other sites but just can't solve the problem.

I've tried following the steps in the following accepted answer but it makes no difference: Django - no such table exception

I've been trying to fix this problem for hours and I'm just not getting anywhere.

Thank you for your help!

Peter
  • 91
  • 1
  • 11
  • From the stack link, you have tried please delete database and migrations except `__init__.py` file inside migration folder then again migrate I think that should work! – Meha Parekh Feb 28 '20 at 12:27
  • Thanks @MehaParekh. I've literally just solved it. See my answer below. – Peter Feb 28 '20 at 12:31

3 Answers3

3

Wow, after hours of getting nowhere, I've literally just solved it!

Turns out I was trying to load data into the sqlite database in urls.py before the database was ready.

makemigrations triggered the loader.load_db() call before migrate was performed. Once I commented out loader.load_db() the migrations worked perfectly! :)

So in summary, make sure you're not trying to load any data into the database until after makemigrations and migrate have been performed.

Peter
  • 91
  • 1
  • 11
1

You are having code in on module level that executed a query further down the stack:

fault = Fault.objects.get(fault__icontains = fault_name)

On module level this code will be executed when the module is analyzed by makemigrations and the resulting query will fail since the database table is not created yet.

As a solution you will have to place that code at a location where it will not be executed on import or you have to handle the exception raised.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • Thanks @Klaus that's very helpful. If I want to automatically load data into the db when starting up the server (i.e. runserver), what's the best approach for that? I clearly need to do it after the models have been loaded / db tables have been set up. Thanks – Peter Feb 28 '20 at 14:17
  • Hi, what `place that code at a location where it will not be executed on import` means in this case? any practical example please? – Fed Mar 18 '21 at 02:29
0

I have faced the same problem when I was trying to change function-based views to class-based views. I tried to assign class variables by calling database query, so that it was trying to make database calls before creating actual tables, after removing this assignment makemigrations worked perfectly.

Here is how my code example, when I was catching this error

class SampleView(LoginRequiredMixin, UpdateView):
    sample_variable = SampleModel.objects.get(active=True)

The way I resolved this problem

class SampleView(LoginRequiredMixin, UpdateView):
    sample_variable = None

    def get_object(self, queryset=None):
        self.sample_variable = SampleModel.objects.get(active=True)
        return super(SampleView, self).get_object(queryset)

If you're facing such an error, check out your views, probably the same mistake caused the error in your code.

In my case, I moved database query and variable assignment to overridden get method and left the variable with None value on initialization

P.S i override get_object for this example, but you can move assignment of the variable to some distinct function

Climber
  • 1
  • 1
  • what do you mean `after removing this assignment?`. Probably you are referring to what you said then in the following paragraph `In my case, I moved database query and variable assignment to overridden get method and left the variable with None value on initialization`. can you please add some code examples? – Fed Mar 17 '21 at 16:12
  • 1
    @FedericoCapaldo Apologies, for being inexplicit, added additional code example of the possible solution to this problem – Climber Mar 18 '21 at 19:15