困惑Django sqlite3"无法打开数据库文件"错误

时间:2016-10-21 20:59:07

标签: python django

现在已经打了几个小时对我的头。

所以这就是故事,我试图在客户端的Windows机器上启动Django-1.9.7(使用Python 2.7)应用程序。我之前已成功安装它,并且自上次成功安装以来,没有对数据库表,模型或远程触摸设置的任何内容进行任何更改。

所以我复制文件,然后运行manage.py migrate以生成必要的表。但是,我收到了这个错误:

错误日志

Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "C:\Softrend\F3KWebApi\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\models.py", line 359, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 269, in alter_unique_together
    self._remake_table(model, override_uniques=new_unique_together)
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 197, in _remake_table
    self.quote_name(model._meta.db_table),
  File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: unable to open database file

以下是相关设置:

settings.py

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

我可以看到db.sqlite3正确创建了它应该在哪里,但这个错误仍然存​​在。任何代码访问这些表的尝试都会导致错误表明表格不存在(例如auth_user)。

我已经查看了与此相关的所有SO问题并遵循了this guide,但我还没有找到解决方案。

请记住,我可以使用完全相同的文件在我自己的计算机上运行完全相同的migrate命令。

感谢任何帮助!

的修改

  • 是的,我已尝试将NAME参数明确设置为文件的绝对目录路径(例如'C:\\webapi\\db.sqlite3'
  • 是的,我已经确认BASE_DIR是正确的。

1 个答案:

答案 0 :(得分:0)

好的,不知道为什么会这样,但是我做了这个并将它整理出来:

manage.py flush
manage.py migrate

之前我手动删除了文件(db.sqlite3),我假设它与manage.py flush相同,但也许我错了。