operational error - no such table - Django, mptt

David J. picture David J. · Feb 24, 2014 · Viewed 11.2k times · Source

I'm running into a DB related problem in Django that I don't understand.

I define an MPTT model like so:

class Image(MPTTModel):
    name = models.CharField(max_length=50)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(self):  
        return self.name

    def rank(self):         leaves = self.get_leafnodes()       if leaves:          rank = leaves[0].get_level() - self.get_level()         else:           rank = 0        return rank

mptt.register(Image, order_insertion_by=['name'])

Then in my views, I attempt a few statements with the model, and I get an OperationalError.

def index(request):

    if request.method == 'POST': 
        image_string = request.POST.get('get_image')
        index = image_string.find('(')
        if index == -1:
            parent = image_string
            child = None
        else:
            parent = image_string[0:index]
            child = image_string[index+1:len(image_string)-1]       
        try:
            images = Image.objects.all()
            image_names = [a.name for a in images]     
        except Image.DoesNotExist:
            return render(request, 'images_app/index.html', {'images':[]}) 
        else:
            parent_model = Image(name=parent)
            parent_model.save()
            child_model = Image(name=child, parent=parent_model)
            child_model.save()              
            return render(request, 'images_app/index.html', {'images':images})

I'm not sure if this is a problem with my view or the way I'm defining the model. According to my understanding, the 'try' expression should make it so that if the code doesn't evaluate, it will simply skip to the exception. Why doesn't this go straight to the exception?

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\djangoprojects\images\images_app\views.py" in index
  17.             if images:
File "C:\Python27\lib\site-packages\django\db\models\query.py" in __nonzero__
  100.         self._fetch_all()
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _fetch_all
  854.             self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py" in iterator
  220.         for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  709.         for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  782.         cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  69.             return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py" in __exit__
  99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  450.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /images/
Exception Value: no such table: images_app_image

Answer

HAL picture HAL · Feb 24, 2014

By looking at the exception value (no such table: images_app_image), I would guess that the actual database table doesn't exist.

Check if the table exists in your database by using the ./manage dbshell command. You can list all tables in the database within the shell with the command .schema or use .schema images_app_image to only show the schema definition for the actual table.

If the table doesn't exist, create it with ./manage syncdb (or use the migrate command if you are using South).