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

时间:2018-08-07 01:13:51

标签: python django django-models operationalerror

问题是我正在使用Django和Products模型制作一个电子商务应用程序。 首先,我添加了字段title,description和price来运行所有命令makemigrations,migrate和一切正常。 现在我在其中添加了一个名为Image的字段,然后在应用makemigrations时出现错误 django.db.utils.OperationalError:无此类列:Products_product.image

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

现在在新项目中,此imagefield迁移在我最初的迁移时运行良好,现在我添加了一个名为featured(BOOLEAN FIELD)的字段,将多个产品标记为featured,现在问题又出现了,与之相同imagefield。

django.db.utils.OperationalError:无此类列:Products_product.featured

由于这个问题,我浪费了大量时间在Internet上进行挖掘,但没有一个在工作?请帮忙吗?

这是我的模型的样子

    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'

2 个答案:

答案 0 :(得分:0)

我建议您使用virtualenv

  • 安装virtualenv
  

virtualenv -p python3 envname

  •   

    源环境名称/ bin /激活

  • 然后在此处安装项目所需的所有软件包。

  • 现在您可以轻松使用迁移

答案 1 :(得分:0)

尝试添加返回自身字符串

def __str__(self):
 return self.featured