For the last few days, I've been trying to sort out this error. When I run my script, I'm getting a Django "OperationalError: unable to open database file" error. From what I've found on this SO question and this similar SO question (along with many a Google search), the most common issue is with someone using a relative path rather than an absolute path, or having some sort of typo in one of the strings.
Here is the code relevant to my paths:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_FILE = os.path.join(BASE_DIR, 'data', 'mydbtester.db')
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DATA_FILE,
}
}
To my understanding, the code above the ...
should be getting the absolute path to the directory two directories up from the current file, then joining it with the data
directory and the database name and file extension. I have checked that this file exists, and it does indeed exist in the stated location; the name matches exactly. I am also absolutely sure that the current user has permissions for this file.
I am also unable to migrate
the database, with the exact same error.
Does anyone know why this is causing the OperationalError, or perhaps if there is something else going on?
Note: I am also having this issue when running in a Docker container.
Much appreciated.
EDIT:
After adding a print(DATA_FILE)
statement under the DATA_FILE declaration, it seems that the BASE_DIR
is only moving one directory up when it needs to move up by two. To see if this was the issue, I've moved the data
directory down one directory, and it started working. However, this is not the way my project should be structured. Any help to get the BASE_DIR
to reference one more directory up?