简体   繁体   English

令人困惑的Django sqlite3“无法打开数据库文件”错误

[英]Perplexing Django sqlite3 “unable to open database file” error

Been banging my head against this for a couple hours now. 现在已经将我的头撞了几个小时。

So here's the story, I'm trying to spin up a Django-1.9.7 (using Python 2.7) application on a client's Windows machine. 所以这是故事,我正在尝试在客户端Windows计算机上启动Django-1.9.7(使用Python 2.7)应用程序。 I've installed it successfully there before, and have made no changes to the database tables, models, or anything remotely touching the settings since the last successful installation. 我之前已经在这里成功安装了它,并且自上次成功安装以来,还没有对数据库表,模型或任何其他涉及设置的内容进行任何更改。

So I copy over the files, then run manage.py migrate to generate the necessary tables. 因此,我复制了文件,然后运行manage.py migrate生成必要的表。 However, I get this error: 但是,我收到此错误:

Error Log 错误记录

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

And here are the relevant settings: 以下是相关设置:

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'),
    }
}

I can see the db.sqlite3 being correctly created where it's supposed to be, but this error still persists. 我可以看到db.sqlite3已正确创建,但该错误仍然存​​在。 Any attempt for code to access these tables results in errors that say the tables don't exist (eg. auth_user ). 任何试图访问这些表的代码都会导致错误,指出该表不存在(例如auth_user )。

I've looked through just about every one of the SO questions related to this and followed this guide but I've yet to find a solution. 我已经仔细研究了与此相关的每个SO问题,并遵循了本指南,但是我还没有找到解决方案。

Keep in mind that I can run the exact same migrate command on my own machine just fine with the exact same files. 请记住,我可以在完全相同的文件上运行完全相同的migrate命令。

Any help is appreciated! 任何帮助表示赞赏!

EDITS 编辑

  • Yes, I've tried explicitly setting the NAME parameter to an absolute directory path to the file (eg. 'C:\\\\webapi\\\\db.sqlite3' ) 是的,我尝试将NAME参数显式设置为文件的绝对目录路径(例如'C:\\\\webapi\\\\db.sqlite3'
  • Yes, I've verified that the BASE_DIR is correct. 是的,我已经验证BASE_DIR是正确的。

Okay, no idea why this works, but I did this and it sorted it out: 好的,不知道为什么会这样,但是我做到了,然后整理出来:

manage.py flush
manage.py migrate

I had manually deleted the file ( db.sqlite3 ) before, which I'm assuming is the same thing as manage.py flush , but maybe I'm wrong. 我之前手动删除了文件( db.sqlite3 ),我认为它与manage.py flush是同一回事,但也许我错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM