Django:django.db.utils.OperationalError: no such column

Django:django.db.utils.OperationalError: no such column

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

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

现在在新项目中,这个图像字段迁移在我最初的迁移中运行良好,现在我添加了一个名为 featured(BOOLEAN FIELD) 的字段来将几个产品标记为特色,现在问题又出现了,同样的问题使用那个图像域。

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

我为了这个问题浪费了很多时间在互联网上搜索,但是 none 的问题还在解决?请帮忙?

这是我的模型的样子

    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'

我建议你使用 virtualenv

  • 安装 virtualenv

virtualenv -p python3 envname

  • source envname/bin/activate

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

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

尝试添加一个 return 自身字符串

def __str__(self):
 return self.featured