Soroush Ghodsi
Soroush Ghodsi

Reputation: 380

Django 1.8 OperationalError: no such column:

I'm using django 1.8 and I'm having problems adding to my models.py. Currently it's:

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 perfectly fine but whenever I try to add to this and then run the server I get

OperationalError: no such column [added element]

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: companies_company.founder

Upvotes: 4

Views: 9826

Answers (5)

bubuwanted02
bubuwanted02

Reputation: 1

Maybe not optimal, but it worked for me doing that (and the default thing did not work):

  1. delete all files apart from the init.py in migrations folder
  2. delete the db.sqlite file
  3. doing: python manage.py runserver (without this one previous to migrate, migrations, that did not work, I think it's linked to the views / templates calling tags, so has to run first)
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
python manage.py createsuperuser 

Upvotes: 0

Raccoondude
Raccoondude

Reputation: 21

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

Then makemigrations, sqlmigrate, and then migrate

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

Upvotes: -1

Naveen
Naveen

Reputation: 21

This type of problem occurs when there are some operations on the model field in some other files such as forms.py or views.py other than models.py when you run makemigrations. If you read the traceback carefully you can figure it out from which file the problem is originating.

For example, if the traceback tells you some complaints in forms.py which may happen to use some of the model fields, just comment out the code that is working on the model fields and rerun makemigrations again. Hopefully it resolves the issue.

If it does, you can then remove the comments that you added before.

Upvotes: 2

Dima  Kudosh
Dima Kudosh

Reputation: 7386

Run in your console this commands:

manage.py makemigrations app_name
manage.py migrate app_name

Every time when you change model in your app you should migrate changes to your db using makemigration and migrate commands. When you adding a new column to your db table you must add value of this column to all existing rows. You can do it by seting default value in your new field in your model. Or set values when run migrate command ( django automatically propose this) You can read about this in docs

Upvotes: 4

Obj3ctiv3_C_88
Obj3ctiv3_C_88

Reputation: 1528

You have to re sync the database using python manage.py makemigrations

Upvotes: 0

Related Questions