简体   繁体   中英

django makemigrations error “no such column”

I am developing a Django project with REST Framework. It all worked well before, but all of a sudden I can't add any new model fields: If I add any new field even if the most simple one like this:

    def_episode = models.IntegerField(blank=True, null=True)

And manage.py makemigrations , this will happen:

Traceback (most recent call last):
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: novelrecorder_novel.def_episode

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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 361, in execute
    self.check()
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\urls\resolvers.py", line 584, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\utils\functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\urls\resolvers.py", line 577, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\daichou\AppData\Local\Programs\Python\Python37-32\lib\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 "D:\MyProjects\Python\Django\env\mysite\mysite\urls.py", line 19, in <module>
    from novelrecorder import views
  File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\views.py", line 19, in <module>
    from novelrecorder.serializers import NovelSerializer, NovelReadOnlySerializer, \
  File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\serializers.py", line 241, in <module>
    class CharacterCreateSerializer(CharacterSerializer):
  File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\serializers.py", line 245, in CharacterCreateSerializer
    param_key_field_name='novel_id'))
  File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\yd_fields.py", line 23, in __init__
    super().__init__(**kwargs)
  File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\yd_fields.py", line 10, in __init__
    super().__init__(**kwargs)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\rest_framework\fields.py", line 1404, in __init__
    self.choices = choices
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\rest_framework\fields.py", line 1440, in _set_choices
    self.grouped_choices = to_choices_dict(choices)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\rest_framework\fields.py", line 142, in to_choices_dict
    for choice in choices:
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\models\query.py", line 274, in __iter__
    self._fetch_all()
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "D:\MyProjects\Python\Django\env\venv\lib\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 "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\MyProjects\Python\Django\env\venv\lib\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 column: novelrecorder_novel.def_episode

Without adding any new fields, everything looks ok and the server works. Tried to remove the field and makemigrations then migrate , "Nothing needs to change or apply"

Parts of settings.py :


INSTALLED_APPS = [
    'novelrecorder.apps.NovelRecorderConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'rest_framework',
    'gunicorn',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

The model I attempted to add a new field model.py :

class Novel(CustomNovelModel):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name='novel_author')
    name = models.CharField(max_length=200)
    is_public = models.BooleanField(default=True)
    def_episode = models.IntegerField(blank=True, null=True)

    class Meta:
        ordering = ['name']
        unique_together = ['author', 'name']

I was searching for a similar problem and came across you question. After reading through my error-log again I noticed that the migrations was stumbling on some frontend and admin code which was causing it to fail.

To 'fix' the issue

  1. I temporary commented-out the code in question
  2. Ran migrations
  3. Deployed everything
  4. Uncommented the code

All seems to be fine.

When I'm looking through your log, I'm seeing a package novelrecorder which seems to cause an error. Try commenting-out that code.

Perhaps this helps you or someone else coming across this issue.

Delete db.sqlite3 from ur database and migrations from ur App. Then try to migrate again..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM