0

I'm having trouble doing a migration with Django 1.10. (python 2.7.6)

While migrating the interpreter look throughout the entire code. In a model form the model related got a custom manager that query an other object which is used to do something for the queryset. But, I receive an OperationalError no such table exists.

Here is my code in the models.py:

class Config(models.Model):
    name = models.CharField(max_length=700)


class Question(models.Model):
    tags = models.ManyToManyField("Tag", blank=True)


class TagManager(models.Manager):

    def get_queryset(self):
        config = Config.objects.first()
        return super(TagManager, self).get_queryset()


class Tag(models.Model):
    name = models.CharField(max_length=700)

    objects = TagManager()

Here is my forms.py:

from django import forms
from models import Question

class QuestionForm(forms.ModelForm):

    class Meta:
        model = Question
        fields = ('tags', )

The problem is caused by the imports

import questions.views

then in questions/views.py

from forms import QuestionForm

Here the stack trace

    Traceback (most recent call last):
  File "/home/user/projects/dev/error/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/base.py", line 342, in execute
    self.check()
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/user/projects/dev/error/error/urls.py", line 19, in <module>
    from questions.views import *
  File "/home/user/projects/dev/error/questions/views.py", line 3, in <module>
    from forms import QuestionForm
  File "/home/user/projects/dev/error/questions/forms.py", line 4, in <module>
    class QuestionForm(forms.ModelForm):
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/forms/models.py", line 247, in __new__
    opts.field_classes)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/forms/models.py", line 166, in fields_for_model
    formfield = f.formfield(**kwargs)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1579, in formfield
    'queryset': self.remote_field.model._default_manager.using(db),
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/user/projects/dev/error/questions/models.py", line 17, in get_queryset
    config = Config.objects.first()
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/query.py", line 556, in first
    objects = list((self if self.ordered else self.order_by('pk'))[:1])
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/query.py", line 256, in __iter__
    self._fetch_all()
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/user/.virtualenvs/ms1.10/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: questions_config
Greg Madro
  • 39
  • 1
  • 7
evgolk
  • 1
  • Have you done `makemigrations` and `migrate`? – voodoo-burger Jan 05 '17 at 13:37
  • Yes, `makemigrations` and `migrate` give me the same `OperationalError`. – evgolk Jan 05 '17 at 13:49
  • Is the app `Questions` (or whatever naming is used) active in your settings file? – SaeX Jan 05 '17 at 14:03
  • Yes, it's in INSTALLED_APPS – evgolk Jan 05 '17 at 14:16
  • Try this `python manage.py migrate --run-syncdb` found in [this answer](http://stackoverflow.com/a/37799885/4038509) – Erion S Jan 05 '17 at 14:30
  • It's still the same error. The command `manage.py check` produce the same error also. – evgolk Jan 05 '17 at 14:49
  • What happens when you remove the custom manager from `Tag`? – voodoo-burger Jan 05 '17 at 15:33
  • Indeed it works. The problem is not custom manager from `Tag`, but this line `config = Config.objects.first()` (it works fine when I comment it). However I need to keep it. – evgolk Jan 05 '17 at 15:47
  • `Config` may be a reserved keyword in Django or Python. Try renaming it to something else. I don't see why you have that line there anyway, since you're not including it in `get_queryset()` in any way. If you need `config` as a variable you should declare it in the view. – voodoo-burger Jan 05 '17 at 16:11
  • Indeed it's useless in this snippet of code. It's not the complete code but I use it in my `get_queryset()`. – evgolk Jan 05 '17 at 16:39

0 Answers0