OperationalError in Django while updating model

39 Views Asked by At

I am facing OperationalError while updating the model in an existing Django project.

These are my installed apps in settings

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "admin1_app.apps.Admin1AppConfig",
]

This is code in admin

from django.contrib import admin
from admin1_app.models import Product
class Product_admin(admin.ModelAdmin):
    list_display=["product_name","product_price","product_qty","product_category"]
admin.site.register(Product,Product_admin)

models.py

from django.db import models
class Product(models.Model):
    product_name=models.CharField(max_length=30)
    product_price=models.IntegerField()
    product_qty=models.IntegerField()
    product_category=models.CharField(max_length=35)
    def __str__(self):
        return self.product_name

Iniatially a program was created with only 3 columns i.e., product_name, product_price, product_qty, and everything was done correctly. Later, I added product_category to this existing project and I also ran makemigrations and migrate commands. But I'm experiencing an OperationalError.

OperationalError at /admin/admin1_app/product/
(1054, "Unknown column 'admin1_app_product.product_category' in 'field list'")
Request Method: GET
Request URL:    http://127.0.0.1:8080/admin/admin1_app/product/
Django Version: 4.1.2
Exception Type: OperationalError
Exception Value:    
(1054, "Unknown column 'admin1_app_product.product_category' in 'field list'")
Exception Location: C:\Users\narendra\anaconda3\lib\site-packages\pymysql\err.py, line 143, in raise_mysql_exception
Raised during:  django.contrib.admin.options.changelist_view
Python Executable:  C:\Users\narendra\anaconda3\python.exe
Python Version: 3.9.12

I tried using the python manage.py migrate --fake command . I don't know how it works but just copied it from some other similar error solution in Stack Overflow, however there is no result I even tried deleting the 0001_initial.py file in the migrations folder and then repeated the makemigrations command, but still of no use.

1

There are 1 best solutions below

0
On

Simply you can migrate manually using below command:

python3 manage.py makemigrations appname

python3 manage.py sqlmigrate appname 0001

python3 manage.py migrate

And see if it solves