我正在使用REST Framework开发Django项目。之前一切都很好,但是突然之间我无法添加任何新的模型字段: 如果我添加任何新字段,即使最简单的字段是这样的:
def_episode = models.IntegerField(blank=True, null=True)
然后manage.py makemigrations
,就会发生这种情况:
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
在不添加任何新字段的情况下,一切正常,服务器正常运行。
试图删除该字段,然后makemigrations
然后删除migrate
,“无需更改或应用”
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',
]
我尝试添加新字段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']
答案 0 :(得分:0)
从ur数据库中删除db.sqlite3,并从ur App中进行迁移。然后尝试再次迁移。.
答案 1 :(得分:0)
我正在寻找类似的问题,并且遇到了您的问题。 再次阅读完错误日志后,我注意到迁移在某些前端和管理代码上受挫,导致迁移失败。
“解决”问题
一切似乎都很好。
当我查看您的日志时,我看到一个软件包novelrecorder
,该软件包似乎会导致错误。尝试注释掉该代码。
也许这可以帮助您或其他人解决此问题。