Opened 18 years ago

Closed 16 years ago

#2631 closed defect (invalid)

pysqlite2.dbapi2.OperationalError: no such table - SQLite needs full path for settings.DATABASE_NAME

Reported by: dev@… Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: trivial Keywords:
Cc: dev@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If the sqlite database is given a relative path in settings.DATABASE_NAME ( e.g. "database.db" ), then any trying to load it from a subdir fails at first database access as pysqlite can't find the table (i.e. it's assuming we're creating the database).

e.g.:

project/databasename.db/
project/somedir/
project/somedir/script.py

script.py looks like:

#!/usr/bin/env python
import sys,os

# Django bootstrap from http://fnordia.org/?q=node/483
project_dir = os.path.abspath('../')
project_name = os.path.basename(project_dir)
sys.path.append(os.path.join(project_dir, '..'))
sys.path.append("..")
sys.path.pop()

os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project_name

from django.conf import settings

from wnmm.monkeys.models import Play
P = Play.objects.get(pk=1)

and does this:

minerva:~/wnmm/data simon$ python script.py  
Traceback (most recent call last):
  File "script.py", line 16, in ?
    P = Play.objects.get(pk=1)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/manager.py", line 67, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py", line 211, in get
    obj_list = list(clone)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py", line 103, in __iter__
    return iter(self._get_data())
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py", line 430, in _get_data
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py", line 172, in iterator
    cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/backends/util.py", line 12, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/backends/sqlite3/base.py", line 79, in execute
    return Database.Cursor.execute(self, query, params)
pysqlite2.dbapi2.OperationalError: no such table: monkeys_play

Fixing this is probably outside the scope of Django as the user should really use the fullpath to the database, but I'm posting it here in case anyone else is having the same problem.

Possible ways of fixing - get the SQLite database wrapper to translate the relative path to the fullpath.

You can easily work around it by doing this in your settings:

from os.path import realpath
DATABASE_NAME = realpath('database.db')

...or in your script, before you import any of your models:

project_dir = os.path.abspath('../') # or path to the dir. that the db should be in.
settings.DATABASE_NAME = os.path.join( project_dir, settings.DATABASE_NAME )

Change History (3)

comment:1 by anonymous, 18 years ago

Cc: dev@… added

comment:2 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedDesign decision needed

It'd be nice to have the wrapper handle this, or at least have this documented.

comment:3 by Jacob, 16 years ago

Resolution: invalid
Status: newclosed

The documentation for DATABASE_NAME already says that "for SQLite, it’s the full path to the database file." I'm not sure how much more clear that can get.

Note: See TracTickets for help on using tickets.
Back to Top