django.db.utils.OperationalError无论我尝试什么 [英] django.db.utils.OperationalError no matter what I try

查看:1012
本文介绍了django.db.utils.OperationalError无论我尝试什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我很累。我想我已经尝试了一切。



我在应用程序的 models.py 中创建了一个模型,并且还向现有模型添加了更多字段具有默认值)。然后我运行 makemigrations 。结果:

  $ python3 manage.py makemigrations 
追溯(最近的最后一次呼叫):
文件/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py,第64行,执行
return self.cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py,第323行,执行
return Database.Cursor.execute(self,query ,params)
sqlite3.OperationalError:没有这样的列:myapp_MyExistingModel.first_new_field

以上异常是以下异常的直接原因:

追溯(最近的call last):
文件manage.py,第14行在< module>
execute_from_command_line(sys.argv)
文件/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py,第353行,execute_from_command_line
utility.execute()
文件/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py,第345行,执行
self.fetch_command (子命令).run_from_argv(self.argv)
文件/usr/local/lib/python3.5/dist-packages/django/core/management/base.py,第348行,在run_from_argv
self.execute(* args,** cmd_options)
文件/usr/local/lib/python3.5/dist-packages/django/core/management/base.py,第398行,执行
self.check()
文件/usr/local/lib/python3.5/dist-packages/django/core/management/base.py,第426行,检查
include_deployment_checks = include_deployment_checks,
文件/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py,第75行,在run_checks
new_errors = check(app_configs = app_configs)
文件/ usr / local / lib / python3.5 / dist-packages / django / core / checking / urls.py,第13行,check_url_config
return check_resolver(resolver)
文件/ usr / local / lib / python3.5 / dist-packages / django / core / checking / urls.py,第23行,check_resolver
for resolver.url_patterns中的pattern:
文件/ usr / local / lib / python3.5 / dist-packages / django / utils / functional.py,第33行,__get__
res = instance .__ dict __ [self.name] = self.func(instance)
文件/ usr / local / lib / python3.5 / dist-packages / django / core / urlresolvers.py,第417行,url_patterns
patterns = getattr(self.urlconf_module,urlpatterns,self.urlconf_module)
文件/usr/local/lib/python3.5/dist-packages/django/utils/functional.py,第33行,__get__
res = instance .__ dict __ [self.name] = self。 func(instance)
文件/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py,第410行,urlconf_module
return import_module(self.urlconf_name)
文件/ usr / lib / python3.5 / importlib / __ init__.py,第126行,import_module
return _bootstrap._gcd_import(name [level:],package,level)
文件< frozen importlib._bootstrap>,第986行,_gcd_import
文件< frozen importlib._bootstrap>,第969行,_find_and_load
文件< frozen importlib._bootstrap>,第958行,_find_and_load_unlocked
文件< frozen importlib._bootstrap>,第673行,在_load_unlocked
文件< frozen importlib._bootstrap_external>,第665行,exec_module
文件< frozen importlib._bootstrap第222行,在_call_with_frames_removed
文件/path/to/myproject/myproject/urls.py中,第49行在< module>
url(r'^ someurl /',include('myapp.urls')),
文件/usr/local/lib/python3.5/dist-packages/django/conf/urls/ __init__.py,第52行,include
urlconf_module = import_module(urlconf_module)
文件/usr/lib/python3.5/importlib/__init__.py,第126行,在import_module
return _bootstrap._gcd_import(name [level:],package,level)
文件< frozen importlib._bootstrap>,第986行,在_gcd_import
文件< frozen importlib._bootstrap> ,第969行,_find_and_load
文件< frozen importlib._bootstrap>,第958行,_find_and_load_unlocked
文件< frozen importlib._bootstrap>,第673行,_load_unlocked
文件< frozen importlib._bootstrap_external>,第665行,exec_module
文件< frozen importlib._bootstrap>,第222行,在_call_with_frames_removed
文件/ path / to / myproject / myapp /urls.py,第2行,在< module>
来自。导入视图
文件/path/to/myproject/myapp/views.py,第8行,位于< module>
existing_model = MyExistingModel.objects.all()[0]
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第297行,在__getitem__
返回列表(qs)[0]
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第258行,在__iter__
self._fetch_all()
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第1074行,在_fetch_all
self._result_cache = list(self.iterator())
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第52行, __iter__
results = compiler.execute_sql()
文件/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py,第848行execute_sql
cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py,第79行,执行
return super(CursorDebugWrapper,self).execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/ db / backends / utils.py,第64行,执行
return self.cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/ django / db / utils.py,第95行,__exit__
six.reraise(dj_exc_type,dj_exc_value,traceback)
文件/usr/local/lib/python3.5/dist-packages/django /utils/six.py,第685行,重新计算
raise value.with_traceback(tb)
文件/usr/local/lib/python3.5/dist-packages/django/db/backends /utils.py,第64行,执行
return self.cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/db /backends/sqlite3/base.py,第323行,执行
return Database.Cursor.execute(self,query,params)
django.db.utils.OperationalError:没有这样的列:myapp_MyExistingModel。 first_new_field

如果重要,我使用夹层CMS,MyExistingModel是其页面类。它只有一个实例。



所以这里是我下一步尝试的:


  1. manage.py migrate --fake

  2. manage.py flush

  3. 重命名数据库,以便创建一个新的数据库。

所有这些都抛出相同的追溯。我进入了应用程序的最后一次迁移。它为我的新模型包含一个 AddModel ,而对于我重新命名的旧模型中的一个字段,它包含一个 RemoveField code> AddField 为我创建的新字段。所以我将它们手动添加到迁移文件中,并重试 manage.py migrate --fake 。相同的追溯。我也试过所有其他命令。相同的追溯。最后,我删除了所有的应用程序迁移,只剩下 __ init __。py 。我还重命名了数据库,以便重新创建它。我重试了 flush makemigrations migrate (with and没有 - 假的)。所有这些都导致了这种追溯:

 追溯(最近的最后一次呼叫):
文件/ usr / local /lib/python3.5/dist-packages/django/db/backends/utils.py,第64行,执行
return self.cursor.execute(sql,params)
文件/ usr /local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py,第323行,执行
return Database.Cursor.execute(self,query,params)
sqlite3.OperationalError:没有这样的表:myapp_MyExistingModel

以上异常是以下异常的直接原因:

追溯(最近的最后一次调用):
文件manage.py,第14行,在< module>
execute_from_command_line(sys.argv)
文件/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py,第353行,execute_from_command_line
utility.execute()
文件/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py,第345行,执行
self.fetch_command (子命令).run_from_argv(self.argv)
文件/usr/local/lib/python3.5/dist-packages/django/core/management/base.py,第348行,在run_from_argv
self.execute(* args,** cmd_options)
文件/usr/local/lib/python3.5/dist-packages/django/core/management/base.py,第398行,执行
self.check()
文件/usr/local/lib/python3.5/dist-packages/django/core/management/base.py,第426行,检查
include_deployment_checks = include_deployment_checks,
文件/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py,行75,在run_checks
new_errors = check(app_configs = app_configs)
文件/ usr / local / lib / python3.5 / dist-packages / django / core / checking / urls.py,第13行,check_url_config
返回check_resolver(resolver)
文件/ usr / local / lib / python3.5 / dist-packages / django / core / checking / urls.py,第23行,check_resolver
for resolver.url_patterns中的pattern:
文件/ usr / local / lib / python3.5 / dist-packages / django / utils / functional.py,第33行,__get__
res = instance .__ dict __ [self.name] = self.func(instance)
文件/ usr / local / lib / python3.5 / dist-packages / django / core / urlresolvers.py,第417行,url_patterns
patterns = getattr(self.urlconf_module,urlpatterns,self.urlconf_module)
文件/usr/local/lib/python3.5/dist-packages/django/utils/functional.py,第33行,__get__
res = instance .__ dict __ [self.name] = self。 func(instance)
文件/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py,第410行,urlconf_module
return import_module(self.urlconf_name)
文件/ usr / lib / python3.5 / importlib / __ init__.py,第126行,import_module
return _bootstrap._gcd_import(name [level:],package,level)
文件< frozen importlib._bootstrap>,第986行,_gcd_import
文件< frozen importlib._bootstrap>,第969行,_find_and_load
文件< frozen importlib._bootstrap>,第958行,_find_and_load_unlocked
文件< frozen importlib._bootstrap>,第673行,在_load_unlocked
文件< frozen importlib._bootstrap_external>,第665行,exec_module
文件< frozen importlib._bootstrap第222行,在_call_with_frames_removed
文件/path/to/myproject/myproject/urls.py中,第49行在< module>
url(r'^ someurl /',include('myapp.urls')),
文件/usr/local/lib/python3.5/dist-packages/django/conf/urls/ __init__.py,第52行,include
urlconf_module = import_module(urlconf_module)
文件/usr/lib/python3.5/importlib/__init__.py,第126行,在import_module
return _bootstrap._gcd_import(name [level:],package,level)
文件< frozen importlib._bootstrap>,第986行,在_gcd_import
文件< frozen importlib._bootstrap> ,第969行,_find_and_load
文件< frozen importlib._bootstrap>,第958行,_find_and_load_unlocked
文件< frozen importlib._bootstrap>,第673行,_load_unlocked
文件< frozen importlib._bootstrap_external>,第665行,exec_module
文件< frozen importlib._bootstrap>,第222行,在_call_with_frames_removed
文件/ path / to / myproject / myapp /urls.py,第2行在< module>
来自。导入视图
文件/path/to/myproject/myapp/views.py,第8行,位于< module>
existing_model = MyExistingModel.objects.all()[0]
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第297行,在__getitem__
返回列表(qs)[0]
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第258行,在__iter__
self._fetch_all()
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第1074行,在_fetch_all
self._result_cache = list(self.iterator())
文件/usr/local/lib/python3.5/dist-packages/django/db/models/query.py,第52行, __iter__
results = compiler.execute_sql()
文件/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py,第848行execute_sql
cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py,第79行,执行
return super(CursorDebugWrapper,self).execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/ db / backends / utils.py,第64行,执行
return self.cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/ django / db / utils.py,第95行,__exit__
six.reraise(dj_exc_type,dj_exc_value,traceback)
文件/usr/local/lib/python3.5/dist-packages/django /utils/six.py,第685行,重新计算
raise value.with_traceback(tb)
文件/usr/local/lib/python3.5/dist-packages/django/db/backends /utils.py,第64行,执行
return self.cursor.execute(sql,params)
文件/usr/local/lib/python3.5/dist-packages/django/db /backends/sqlite3/base.py,行323,执行
return Database.Cursor.execute(self,query,params)
django.db.utils.OperationalError:没有这样的表:myapp_MyExistingModel

在这一点上,我不知道接下来要尝试什么,除了重新开始。任何建议是赞赏。

解决方案

我想出来了!经验教训:阅读整个堆栈跟踪,而不仅仅是最后一行。在这种情况下,进攻线位于我的 views.py 中:

 文件/path/to/myproject/myapp/views.py,第8行在< module> 
existing_model = MyExistingModel.objects.all()[0]

像我说的, code> MyExistingModel 只有一个实例,此行将该实例设置为本地变量 existing_model ,用于 views.py 代码。



我仍然不明白为什么 makemigrations 需要运行 views.py 。但它显然是!这样做, MyExistingModel 是从 models.py (包括)的新字段导入的。但这些领域尚未迁移。因此,OperationalError。


OK, I'm stumped. I think I've tried everything.

I created a model in an app's models.py and also added some more fields to an existing model (all with default values). Then I ran makemigrations. The result:

$ python3 manage.py makemigrations
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: myapp_MyExistingModel.first_new_field

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

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/path/to/myproject/myproject/urls.py", line 49, in <module>
    url(r'^someurl/', include('myapp.urls')),
  File "/usr/local/lib/python3.5/dist-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/path/to/myproject/myapp/urls.py", line 2, in <module>
    from . import views
  File "/path/to/myproject/myapp/views.py", line 8, in <module>
    existing_model = MyExistingModel.objects.all()[0]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 297, in __getitem__
    return list(qs)[0]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute
        return Database.Cursor.execute(self, query, params)
    django.db.utils.OperationalError: no such column: myapp_MyExistingModel.first_new_field

If it matters, I'm using Mezzanine CMS and MyExistingModel is an extension of its Page class. It only has one instance.

So here's what I tried next:

  1. manage.py migrate --fake
  2. manage.py flush
  3. Renaming my database so a new one would be created

All of these threw the same traceback. I went into the last migration for the app. It contained an AddModel for my new model and a RemoveField for a field in the old model I renamed, but no AddField for the new fields I created. So I added them to the migrations file manually and retried manage.py migrate --fake. Same traceback. I tried all the other commands too. Same traceback.

Finally, I deleted all the app's migrations, leaving just __init__.py. I also renamed the database so it would be recreated. I retried flush, makemigrations, and migrate (with and without --fake). All of these resulted in this traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: myapp_MyExistingModel

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

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/path/to/myproject/myproject/urls.py", line 49, in <module>
    url(r'^someurl/', include('myapp.urls')),
  File "/usr/local/lib/python3.5/dist-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/path/to/myproject/myapp/urls.py", line 2, in <module>
    from . import views
  File "/path/to/myproject/myapp/views.py", line 8, in <module>
    existing_model = MyExistingModel.objects.all()[0]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 297, in __getitem__
    return list(qs)[0]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: myapp_MyExistingModel

At this point, I have no idea what to even try next, except start over. Any suggestions are appreciated.

解决方案

I figured it out! Lesson learned: read the whole stacktrace, not just the last line. In this case, the offensive line was in my views.py:

File "/path/to/myproject/myapp/views.py", line 8, in <module>
  existing_model = MyExistingModel.objects.all()[0]

Like I said, MyExistingModel has only one instance, and this line set that instance to the local variable existing_model for use in the views.py code.

I still don't understand why makemigrations would need to run views.py. But it does, apparently! In doing so, MyExistingModel was imported from models.py, including the new fields. But those fields hadn't yet been migrated. Thus, OperationalError.

这篇关于django.db.utils.OperationalError无论我尝试什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆