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

Viewed 42650

environment DJANGO VERSION 1.9 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:    http://127.0.0.1:8000/
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:    
['c:\\FairyBBS',
 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python27\\site-packages\\setuptools-5.4.1-py2.7.egg',
 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python27\\site-packages\\urlobject-2.4.0-py2.7.egg',
 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python27\\site-packages\\djangorestframework-0.4.0-py2.7.egg',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python27\\site-packages',
 'C:\\Python27\\lib\\site-packages']
Server time:    星期四, 17 三月 2016 14:08:04 +0800
7 Answers

Well the problem is NOT with your makemigrations command or models.py. It is because you have probably imported your class in the model (your database in this case) in on of your views.py files and the problem is with that. If you read the all of the error message then you can understand that easily.

Just try commenting that importing part and run your python.manage.py makemigrations and python manage.py migrate commands then you can uncomment your import in your views.py file

Hope this was useful for others as well

All, that u need: download «DB Browser for SQLite» and:

  1. Copy your ‘db.sqlite3’ to different Folder.
  2. Delete your ‘db.sqlite3’.
  3. Run ‘python3 manage.py make migrations’, then ‘python3 manage.py migrate’.
  4. Open your new ‘db.sqlite3’ in «DB Browser for SQLite» and find table ‘account_profile’
  5. Then do like 1st image. U should export ‘account-profile’ table to CSV file.
  6. Delete your new ‘db.sqlite3’ and return you old ‘db.sqlite3’
  7. Open your DB in «DB Browser for SQLite», then «File—>Import—>Tables from file CSV..» and choose ‘account_profile.csv’
  8. Then U will see, that in this table u should change columns ‘field1’ to ‘id’ and ‘field2’ to ‘user_id’. I did it on 2nd and 3rd images.
  9. Last, what should u do: Look at ‘id’ of your admin in the table ‘auth_user’ and change on this value in the ‘account_profile table’
  10. Delete your Admin-Model in DB(auth_user table)

Yeah, 5-10 minutes to do it, but all is work)

image1

image2

image3

result

Easyes Solution: RENAME the field makemigrations migrate RENAME back makemigrations migrate

Related