有没有办法解决 django 上的数据库问题?

is there a way to fix a database isssue on django?

我不确定是否删除了一些 pycache 文件并弄乱了我的网站,或者我是否从 git 中提取了一些文件更改了我的文件夹,但我遇到了数据库连接问题。

我尝试了 makemigrations、migrate 和 runserver,但每次都遇到同样的错误。我无法卸载 wagtail 或 django,因为它无法创建进程。我有一种可怕的感觉,可能是时候结束这个项目并重新开始了。

这里是错误

self.connection = self.get_new_connection(conn_params)
  File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 194, in get_new_connection
    conn = Database.connect(**conn_params)
sqlite3.OperationalError: unable to open database file

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

Traceback (most recent call last):
  File "..\threading.py", line 926, in _bootstrap_inner
    self.run()
File "..\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "..\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "..\runserver.py", line 120, in inner_run
    self.check_migrations()
  File "..\base.py", line 453, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "..\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "..\loader.py", line 49, in __init__
    self.build_graph()
  File "..\loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "..\recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "..\recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "..\base.py", line 256, in cursor
    return self._cursor()
  File "..\base.py", line 233, in _cursor
    self.ensure_connection()
  File "..\base.py", line 217, in ensure_connection
    self.connect()
  File "..\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "..\base.py", line 217, in ensure_connection
    self.connect()
  File "..\venv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 194, in get_new_connection
    conn = Database.connect(**conn_params)
django.db.utils.OperationalError: unable to open database file

底线是 Git 如果使用不当,版本控制是不可靠的。感谢任何帮助。

请务必检查您的数据库设置 (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' 的(数据库)文件放置在比包含 this/the settings.py 文件的文件夹高一级的文件夹中。