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

+3


source to share


4 answers


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

+3


source


You need to resynchronize the database with python manage.py makemigrations



0


source


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.

0


source


I found that clearing all data with python3 manage.py sqlflush

and thenpython3 manage.py flush

Then do a migration, sqlmigrate and then migrate

This will delete all data in the SQL database including users and objects

0


source







All Articles