首页 > 解决方案 > Django:django.db.utils.OperationalError:没有这样的列

问题描述

问题是我正在使用 Django 并使用 Products 模型制作电子商务应用程序。首先,我添加了字段标题、描述和价格,运行了所有命令 makemigrations、migrate 并且一切正常。现在我添加了一个名为 Image 的字段,然后在应用 makemigrations 时出现错误 django.db.utils.OperationalError: no such column: Products_product.image

我对此应用了所有建议的解决方案,包括删除数据库并再次重新创建它以及从迁移文件夹中删除迁移历史记录,但它们仍然没有工作,所以我不得不再次创建一个新项目。

现在在新项目中,这个 imagefield 迁移在我最初的迁移中运行良好,现在我添加了一个名为 features(BOOLEAN FIELD) 的字段来将几个产品标记为特色,现在问题再次出现,与该 imagefield 相同的问题。

django.db.utils.OperationalError:没有这样的列:Products_product.featured

我浪费了很多时间在互联网上挖掘这个问题,但他们都没有工作?请帮忙?

这就是我的模型的样子

    class Product(models.Model):
       title = models.CharField(max_length = 100)
       description = models.TextField()
       price =models.DecimalField(decimal_places=2,max_digits=20,default=39.99)
       image = models.ImageField(upload_to=upload_image_path,null=True,blank=True) 
       featured = models.BooleanField(default=False)

以下是显示的完整错误:

  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\models\query.py", line 248, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\models\query.py", line 272, in __iter__
self._fetch_all()
 File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\models\query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\models\sql\compiler.py", line 1067, in execute_sql
cursor.execute(sql, params)
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
 File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
 File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
 File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
  File "C:\Users\vipul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.4-py3.6.egg\django\db\backends\sqlite3\base.py", line 303, in execute
 return Database.Cursor.execute(self, query, params)
 django.db.utils.OperationalError: no such column: Products_product.featured'

标签: pythondjangodjango-modelsoperationalerror

解决方案


尝试添加一个返回自我字符串

def __str__(self):
 return self.featured

推荐阅读