[Solved] django.db.utils.OperationalError: table has no column named

[Solved] django.db.utils.OperationalError: table has no column named

Are you getting the following error while executing makemigrations or migrate command?

    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: table main_model has no column named column_name

I face this issue many times during working on Django app project development, especially when I add a new column to the Python Django models.py file.

I tried many things. And got this issue fixed with the steps I’m sharing with you here.

Step 1: Comment all your code changes and Run the Django migration

First of all, comment all your code changes, especially the changes inside the models.py.

Once you comment on newly added fields in models.py, newly added fields will be removed.

Now, do the migrations by running the following commands.

python manage.py makemigrations
python manage.py migrate

Hopefully, your application will run successfully.

Step 2: Uncomment newly added Django model fields

Now, just uncomment the newly added fields in models.py

Do the migration.

python manage.py makemigrations
python manage.py migrate

And run the application.

python manage.py runserver

Holy cow! You will not see the error you were getting earlier.

Step 3: Uncomment all code changes

Now, uncomment all the commented code. You will not face an earlier issue “django.db.utils.OperationalError: table has no column”. You’re application will be running as smoothly as butter. 😀

If you are still facing challenges, write me in the comment section below. I will help you at my best. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *