[Fixed]-Django beginner problem: manage.py dbsync

21👍

In settings.py are you using a relative path to the sqlite file?

If you are, try changing that to an absolute path.

i.e. instead of:

~/project/mydata.db

use

/home/user/project/mydata.db

17👍

This can also happen if your database name is the same as your project name. To fix it just change the name of your db e.g. by adding a .db to the NAME. So if your DATABASE NAME in settings.py was ‘epic_project’ change it to ‘epic_project.db’


Long and boring explanation:

If you start your project by running:

django startproject epic_project

your folder structure will be like this:

  • /path/to/epic_project/
    • manage.py
    • epic_project/
      • settings.py

if then in your settings.py you set your database as:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'epic_project',
        ...
    }
}

when

python manage.py syncdb 

runs it tries to open or create a sqlite db file at /path/to/epic_project/epic_project, but there is a directory there, so it says “oh ok the path exists already, let’s open it as an sqlite db”, unfortunately for sqlite it’s a directory and not a DB, so it cries and django presents these tears to you as “sqlite3.OperationalError: unable to open database file”

👤JobJob

9👍

might be a permission problem, does your user have sufficient right to write on the folder? for example if you do

sudo python manage.py syncdb

instead, does it work?

5👍

For Google’s sake:

The path to the database must be the full path to the file — not just the directory where the file lives.

👤Raj

3👍

I had the same problem on Windows then realized that syncdb would not create the specified folder if it didn’t already exist. I had specified c:/mysite/db/sqlite3.db in settings but the /db/ folder didn’t exist. Created it in terminal then re-ran syncdb successfully.

👤Drake

2👍

I had the same problem two days ago. I solved it by setting the ‘NAME’ in the DATABASE dictionary (settings.py) to the absolute path.
For example.

settings.py

DATABASES = {
    'default' : {
        'ENGINE' : 'django.db.backends.sqlite3',
        'NAME' : DATABASE_PATH,
    }
}

here you can set the DATABASE_PATH at the top of the settings.py as such
(Not sure if this will work for windows)

SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir))
DATABASE_PATH = os.path.abspath(os.path.join(PROJECT_PATH, 'your-database-name'))

For Windows you might have to use the replace method. (Not sure .. But you can try it out as follows)

PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir).replace('\\', '/'))

Same goes for the DATABASE_PATH.
PS. Correct me if I am wrong.

0👍

Similar to answer from user104264 – different perspective…

Currently following the YouTube tutorial I had the same error. Seems to me while running the manage.py in a virtualenv django project directory …path to virtual env…/django_project/, the database name inside /myapp/settings.py was simply ‘storage.db’ so

(django-v)…path to virtual env project…/django_project/>python manage.py syncdb

created …path to virtual env project…/django_project/storage.db

-Bill

0👍

If you are specifying a full path to db file and are still having issues, try putting an r before the string.

ie.

r'C:\home\usr\mysite\sqlite3.db'

0👍

The problem I was running into was a bootstrapping issue where some model lookups were being done at import (outside of any class or function). Per the docs, this is a bad idea.

👤Tom

Leave a comment