最初的Django迁移时没有这样的表

时间:2019-01-13 03:13:46

标签: django sqlite djongo

背景故事:

我正在创建测验应用程序。首先,我想通过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

1 个答案:

答案 0 :(得分:0)

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

您不应执行以下任何一项操作:不要将测试文件导入到您的实际代码中,即使在测试文件中也不在模块级别执行db-modify动作,它们也应在称为的函数中完成必要时。