【问题标题】:operational error - no such table - Django, mptt操作错误 - 没有这样的表 - Django,mptt
【发布时间】:2014-03-26 18:42:08
【问题描述】:

我在 Django 中遇到了一个我不理解的与数据库相关的问题。

我这样定义一个 MPTT 模型:

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'])

然后在我看来,我尝试对模型执行一些语句,然后得到一个 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})

我不确定这是我的观点还是我定义模型的方式有问题。根据我的理解,'try' 表达式应该这样,如果代码没有计算,它会简单地跳到异常。为什么这不直接进入异常?

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

【问题讨论】:

    标签: python django sqlite django-mptt operationalerror


    【解决方案1】:

    通过查看异常值(no such table: images_app_image),我猜测实际的数据库表不存在。

    使用./manage dbshell 命令检查该表是否存在于您的数据库中。您可以在 shell 中使用命令 .schema 列出数据库中的所有表,或者使用 .schema images_app_image 仅显示实际表的架构定义。

    如果该表不存在,请使用 ./manage syncdb 创建它(如果您使用的是 South,请使用 migrate 命令)。

    【讨论】:

      【解决方案2】:

      试试python manage.py syncdb

      如果你得到

      未知命令:'syncdb'

      你可以试试

      python manage.py migrate --run-syncdb
      

      【讨论】:

        猜你喜欢
        • 2015-07-31
        • 2016-01-05
        • 1970-01-01
        • 1970-01-01
        • 2017-03-12
        • 1970-01-01
        • 1970-01-01
        • 2017-03-27
        • 2013-10-21
        相关资源
        最近更新 更多