1

背后故事:

我正在创建一个测验应用程序。我首先想通过 Django 包将 Django (v=2.1.4) 与 Mongo 一起使用。一切正常,直到它没有,所以我决定完全放弃 Django 和 mongo 并回到 SQLite。我只在它相关的情况下提及它,但我不认为它是相关的。

我制作了该项目的副本,然后删除了原始项目。然后,我创建了一个具有相同名称和所有相同应用程序的新项目。这创建了一堆带有空白文件的新文件夹。我将原始副本移至这些文件夹(迁移文件除外)并替换了具有相同名称的空白文件。

我将mysite/settings.py文件改回默认读取:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3', 
    'NAME': os.path.join(BASE_DIR, 'hoquidb'), 
  }
}

据我所知,这会创建一个新项目,为它的初始迁移做好准备,它将使用 sqlite3。

问题:

所以我尝试进行初始迁移并收到一条神秘的错误消息:

    (base) C:[redacted]\testabode\Hoqui\hoqui>python manage.py migrate
Traceback (most recent call last):
  File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:[redacted]\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: realestate_sentence_beginning

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

    Traceback (most recent call last):
      File "manage.py", line 15, in <module>
        execute_from_command_line(sys.argv)
      File "C:[redacted]\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
        utility.execute()
      File "C:[redacted]\lib\site-packages\django\core\management\__init__.py", line 375, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
        self.execute(*args, **cmd_options)
      File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 350, in execute
        self.check()
      File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 379, in check
        include_deployment_checks=include_deployment_checks,
      File "C:[redacted]\lib\site-packages\django\core\management\commands\migrate.py", line 60, in _run_checks
        issues.extend(super()._run_checks(**kwargs))
      File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
        return checks.run_checks(**kwargs)
      File "C:[redacted]\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
        new_errors = check(app_configs=app_configs)
      File "C:[redacted]\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
        return check_resolver(resolver)
      File "C:[redacted]\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
        return check_method()
      File "C:[redacted]\lib\site-packages\django\urls\resolvers.py", line 396, in check
        for pattern in self.url_patterns:
      File "C:[redacted]\lib\site-packages\django\utils\functional.py", line 37, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "C:[redacted]\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
      File "C:[redacted]\lib\site-packages\django\utils\functional.py", line 37, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "C:[redacted]\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
        return import_module(self.urlconf_name)
      File "C:[redacted]\lib\importlib\__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 978, in _gcd_import
      File "<frozen importlib._bootstrap>", line 961, in _find_and_load
      File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
      File "C:[redacted]\testabode\Hoqui\hoqui\hoqui\urls.py", line 24, in <module>
        path('quiz_taker/', include('quiz_taker.urls')),
      File "C:[redacted]\lib\site-packages\django\urls\conf.py", line 34, in include
        urlconf_module = import_module(urlconf_module)
      File "C:[redacted]\lib\importlib\__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 978, in _gcd_import
      File "<frozen importlib._bootstrap>", line 961, in _find_and_load
      File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
      File "C:[redacted]\testabode\Hoqui\hoqui\quiz_taker\urls.py", line 3, in <module>
        from . import views
      File "C:[redacted]\testabode\Hoqui\hoqui\quiz_taker\views.py", line 2, in <module>
        from quiz_maker.tests.generate_questions import main as question_list_func
      File "C:[redacted]\testabode\Hoqui\hoqui\quiz_maker\tests\generate_questions.py", line 22, in <module>
        sentence_beginning(text=first_words_of_sentence,category='Location').save()
      File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 718, in save
        force_update=force_update, update_fields=update_fields)
      File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 748, in save_base
        updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
      File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 831, in _save_table
        result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
      File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 869, in _do_insert
        using=using, raw=raw)
      File "C:[redacted]\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "C:[redacted]\lib\site-packages\django\db\models\query.py", line 1136, in _insert
        return query.get_compiler(using=using).execute_sql(return_id)
      File "C:[redacted]\lib\site-packages\django\db\models\sql\compiler.py", line 1289, in execute_sql
        cursor.execute(sql, params)
      File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 100, in execute
        return super().execute(sql, params)
      File "C:[redacted]\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:[redacted]\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
        return executor(sql, params, many, context)
      File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
        return self.cursor.execute(sql, params)
      File "C:[redacted]\lib\site-packages\django\db\utils.py", line 89, in __exit__
        raise dj_exc_value.with_traceback(traceback) from exc_value
      File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
        return self.cursor.execute(sql, params)
      File "C:[redacted]\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
        return Database.Cursor.execute(self, query, params)
    django.db.utils.OperationalError: no such table: realestate_sentence_beginning
4

1 回答 1

0

出于某种原因,您将测试文件quiz_maker.tests.generate_questions导入到您的视图中。该文件在顶层具有尝试将元素保存到数据库中的代码。但是由于该代码是在导入时执行的,因此迁移还没有机会运行,并且没有创建表。

您不应该做任何这些事情:不要将测试文件导入到您的实际代码中,即使在测试文件中也不要在模块级别执行 db-modifying 操作,它们应该在必要时调用的函数中完成。

于 2019-01-13T09:17:44.610 回答