“没有这样的列”将字段添加到模型后 [英] "no such column" after adding a field to the model

查看:86
本文介绍了“没有这样的列”将字段添加到模型后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境
DJANGO版本1.9
Python 2.7.6

environment DJANGO VERSION 1.9 Python 2.7.6

我在models.py中的模型类中添加了一个字段(分数)

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

但是得到了

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


推荐答案

快速修复。

首先删除字段分数,然后

python manage.py makemigrations
python manage.py migrate

如果发生任何错误

python manage.py migrate --fake

然后添加字段分数。然后

python manage.py makemigrations
python manage.py migrate

希望这会有所帮助。

这篇关于“没有这样的列”将字段添加到模型后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆