简体   繁体   中英

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

The problem is I am making an eCommerce application using Django and using a Products model. First I added the fields title,description and price ran all the commands makemigrations,migrate and everything was working fine. Now I added a field called Image to it and then when going to apply makemigrations it gives an error django.db.utils.OperationalError: no such column: Products_product.image

I applied all the proposed solutions to this including deleting the database and again recreating it and also deleting the migrations history from the migrations folder but still they didn't work so I had to again make a new project.

Now in new project this imagefield migrations worked fine at my initial migrations , and now i added a field called featured(BOOLEAN FIELD) to mark several products as featured , and now the problem arises again, the same problem that was with that imagefield.

django.db.utils.OperationalError: no such column: Products_product.featured

I wasted a whole lot of time digging the internet for this problem but none of them are working?Please help ?

This is how my model looks like

    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)

Following is the complete error shown:

  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'

I Would advice you use virtualenv

  • install virtualenv

virtualenv -p python3 envname

  • source envname/bin/activate

  • then install here all packages that you need for your project.

  • now you can easily use migration

Try adding a return self string

def __str__(self):
 return self.featured

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM