ming ming - 1 year ago 52
Python Question

"no such column" after adding a field to the model

Python 2.7.6

I added a field (scores) to a model class in models.py like this

from django.db import models
from django.contrib.auth.models import User
import urllib
import hashlib

class profile(models.Model):
user = models.OneToOneField(User)
nickname = models.CharField(max_length=12, blank=True, null=True)
use_gravatar = models.BooleanField(default=True)
location = models.CharField(max_length=20, blank=True, null=True)
avatar_url = models.URLField(blank=True, null=True)
website = models.URLField(blank=True, null=True)
**scores = models.IntegerField(default = 0)**

and I run the following commands to sync the database.

python manage.py makemigrations
python manage.py migrate

But got this

OperationalError at /
no such column: account_profile.scores
Request Method: GET
Request URL:
Django Version: 1.8.4
Exception Type: OperationalError
Exception Value:
no such column: account_profile.scores
Exception Location: C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 318
Python Executable: C:\Python27\python.exe
Python Version: 2.7.5
Python Path:
Server time: 星期四, 17 三月 2016 14:08:04 +0800

Answer Source

Fast fix.

First remove field scores, Then

python manage.py makemigrations
python manage.py migrate

If any error happens

python manage.py migrate --fake

Then add field scores.Then

python manage.py makemigrations
python manage.py migrate

Hope this helps.