OperationalError: no such table but database and migrations folder were deleted
OperationalError: no such table but database and migrations folder were deleted
与此非常相似question:
我有一个脚本:
- 将所有数据转储到json、
- 删除
database.sqlite3
文件和 migrations
文件夹
- 执行:
- python 制作迁移应用程序
- python manage.py 迁移应用程序
- python manage.py makemigrations
- python manage.py 迁移
- 加载来自json
的所有数据
我在开发东西和经常重新启动时经常使用它。现在即使是 makemigrations app
命令也会失败并显示 OperationalError: no such table
.
有趣的是,当我注释掉这个 table 的所有外观时(但模型仍在 models.py
中),一切正常。如果我然后删除评论一切正常。我错过了什么?
回溯:
PS ..\projectname> python manage.py migrate appname
Traceback (most recent call last):
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: appname_unmigrateablemodel
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "..\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "..\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "..\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "..\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "..\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "..\venv\lib\site-packages\django\core\management\commands\migrate.py", line 75, in handle
self.check(databases=[database])
File "..\venv\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "..\venv\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "..\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "..\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "..\venv\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "..\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "..\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "..\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "..\venv\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "..\projectname\projectname\urls.py", line 24, in <module>
path('', include('appname.urls')), ## must be the last ! line in the URLS file because of the empty argument #
File "..\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "..\projectname\appname\urls.py", line 2, in <module>
from . import views
File "..\projectname\appname\views.py", line 15, in <module>
from .external.server_api import APISERVICE
File "..\projectname\appname\external\hiveserver_api.py", line 10, in <module>
USB_ETHERNET_ADAPTER_MAC_LIST = [A.mac for A in unmigrateablemodel.objects.all()]
File "..\venv\lib\site-packages\django\db\models\query.py", line 280, in __iter__
self._fetch_all()
File "..\venv\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "..\venv\lib\site-packages\django\db\models\query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "..\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: appname_unmigrateablemodel
回溯中的这一部分:
File "..\projectname\appname\views.py", line 15, in <module>
from .external.server_api import APISERVICE
File "..\projectname\appname\external\hiveserver_api.py", line 10, in <module>
USB_ETHERNET_ADAPTER_MAC_LIST = [A.mac for A in unmigrateablemodel.objects.all()]
告诉我们您正在 hiveserver_api.py
模块的导入期间进行查询。
由于空数据库中没有表,导入该模块总是会失败,它是通过views
导入的,它是通过urls
导入的,因为应用程序在INSTALLED_APPS
.
您需要重构该代码,使其在导入时不执行该查询,或者只是暂时执行:
USB_ETHERNET_ADAPTER_MAC_LIST = []
这也意味着您的应用程序中存在错误; MAC 地址列表将永远不会在您的视图中更新until/unless 应用程序已重新加载。
顺便说一句,
USB_ETHERNET_ADAPTER_MAC_LIST = list(
unmigrateablemodel.objects.all().values_list("mac", flat=True)
)
是做同样事情的有效方法。
与此非常相似question:
我有一个脚本:
- 将所有数据转储到json、
- 删除
database.sqlite3
文件和migrations
文件夹 - 执行:
- python 制作迁移应用程序
- python manage.py 迁移应用程序
- python manage.py makemigrations
- python manage.py 迁移
- 加载来自json 的所有数据
我在开发东西和经常重新启动时经常使用它。现在即使是 makemigrations app
命令也会失败并显示 OperationalError: no such table
.
有趣的是,当我注释掉这个 table 的所有外观时(但模型仍在 models.py
中),一切正常。如果我然后删除评论一切正常。我错过了什么?
回溯:
PS ..\projectname> python manage.py migrate appname
Traceback (most recent call last):
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: appname_unmigrateablemodel
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "..\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "..\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "..\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "..\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "..\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "..\venv\lib\site-packages\django\core\management\commands\migrate.py", line 75, in handle
self.check(databases=[database])
File "..\venv\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "..\venv\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "..\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "..\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "..\venv\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "..\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "..\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "..\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "..\venv\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "..\projectname\projectname\urls.py", line 24, in <module>
path('', include('appname.urls')), ## must be the last ! line in the URLS file because of the empty argument #
File "..\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "..\projectname\appname\urls.py", line 2, in <module>
from . import views
File "..\projectname\appname\views.py", line 15, in <module>
from .external.server_api import APISERVICE
File "..\projectname\appname\external\hiveserver_api.py", line 10, in <module>
USB_ETHERNET_ADAPTER_MAC_LIST = [A.mac for A in unmigrateablemodel.objects.all()]
File "..\venv\lib\site-packages\django\db\models\query.py", line 280, in __iter__
self._fetch_all()
File "..\venv\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "..\venv\lib\site-packages\django\db\models\query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "..\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: appname_unmigrateablemodel
回溯中的这一部分:
File "..\projectname\appname\views.py", line 15, in <module>
from .external.server_api import APISERVICE
File "..\projectname\appname\external\hiveserver_api.py", line 10, in <module>
USB_ETHERNET_ADAPTER_MAC_LIST = [A.mac for A in unmigrateablemodel.objects.all()]
告诉我们您正在 hiveserver_api.py
模块的导入期间进行查询。
由于空数据库中没有表,导入该模块总是会失败,它是通过views
导入的,它是通过urls
导入的,因为应用程序在INSTALLED_APPS
.
您需要重构该代码,使其在导入时不执行该查询,或者只是暂时执行:
USB_ETHERNET_ADAPTER_MAC_LIST = []
这也意味着您的应用程序中存在错误; MAC 地址列表将永远不会在您的视图中更新until/unless 应用程序已重新加载。
顺便说一句,
USB_ETHERNET_ADAPTER_MAC_LIST = list(
unmigrateablemodel.objects.all().values_list("mac", flat=True)
)
是做同样事情的有效方法。