不能再删除数据库并在 Django 中重新应用迁移?

Can no longer drop database and reapply migrations in Django?

在多次编写我的应用程序的过程中,我不得不删除我的数据库并重新 运行 我的迁移。这总是通过简单地删除数据库文件然后 运行ning manage.py migrate 来工作。但是现在当我尝试这样做时,我收到一条错误消息 django.db.utils.OperationalError: no such table: Planner_course,其中 Planner 是我的应用程序名称,Course 是我的 class 模式之一。我所做的本质上是 。我不确定为什么不到一周前我能够使用这种方法重置我的数据库,但现在我不能。有人有什么想法吗?我在 运行 makemigrations migrate 甚至 runserver 时都会收到错误消息,每次它都会简单地重新制作数据库并在我重新运行没有数据库的迁移时应用迁移.这是完整的错误代码:

"C:\Program Files\JetBrains\PyCharm 2019.3.3\bin\runnerw64.exe" C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\python.exe "C:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pycharm\django_manage.py" migrate C:/Users/cabra/OneDrive/Desktop/CoursePlanner
Tracking file by folder pattern:  migrations
Traceback (most recent call last):
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: Planner_course

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

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pycharm\django_manage.py", line 52, in <module>
    run_command()
  File "C:\Program Files\JetBrains\PyCharm 2019.3.3\plugins\python\helpers\pycharm\django_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 206, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:/Users/cabra/OneDrive/Desktop/CoursePlanner\manage.py", line 21, in <module>
    main()
  File "C:/Users/cabra/OneDrive/Desktop/CoursePlanner\manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 366, in execute
    self.check()
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = self._run_checks(
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\migrate.py", line 64, in _run_checks
    issues.extend(super()._run_checks(**kwargs))
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resolvers.py", line 407, in check
    for pattern in self.url_patterns:
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\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 "C:/Users/cabra/OneDrive/Desktop/CoursePlanner\CoursePlanner\urls.py", line 21, in <module>
    path('', include('Planner.urls'))
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\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 "C:/Users/cabra/OneDrive/Desktop/CoursePlanner\Planner\urls.py", line 2, in <module>
    from . import views
  File "C:/Users/cabra/OneDrive/Desktop/CoursePlanner\Planner\views.py", line 15, in <module>
    for course in courses:
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 276, in __iter__
    self._fetch_all()
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\compiler.py", line 1144, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\cabra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: Planner_course

Process finished with exit code 1

我最终通过擦除 views.py 的所有导入解决了这个问题,出于某种原因。我对处于这种情况的其他人的建议:

  1. 创建项目文件夹的备份
  2. 从项目文件夹中删除数据库
  3. 清除 views.py 的所有导入。例如,这可能会导入您的 urls.pymodels.py.
  4. 然后,运行 migrations 如果您保留迁移,或者 makemigrations 如果您没有

  5. 这应该会重建您的数据库。将这个新数据库复制到您会记得的某个文件夹中

  6. 从备份中恢复所有文件

  7. 将新数据库复制到恢复的项目中(替换旧数据库)

干杯