Django 1.8 OperationalError: No such column:
I am using django 1.8 and am having trouble adding it to my models.py. Currently these are:
from django.db import models
# Create your models here.
class Company(models.Model):
role = models.CharField(max_length=32, blank=True)
name = models.CharField(max_length=70, blank=True)
and it works great, but whenever I try to add to this and then start the server I get
OperationalError: no such column [added item]
For example, I added founder = models.CharField(max_length=200, blank=True)
and I ran the program and I got
django.db.utils.OperationalError: no such column: company_company.founder
source to share
Run the following commands in the console:
manage.py makemigrations app_name
manage.py migrate app_name
Every time you change the model in your application, you have to migrate the changes to your db using the makemigration and migrate commands. When you add a new column to your db table, you must add the value of that column to all existing rows. You can do this by setting a default value in your new field in your model. Or set the values ββwhen executing the migrate command (django will automatically suggest this) You can read about it in the docs
source to share
This type of problem occurs if at startup makemigrations
there are some operations with the model field in some other files such as forms.py
or views.py
besides models.py
. If you read the trace carefully, you can understand from which file the problem is originating.
For example, if a traceback tells you some complaints forms.py
that some of the model fields might be using, just comment out the code that works on the model fields and run again makemigrations
. Hope this solves the problem.
If so, you can delete the comments that you added earlier.
source to share