Django 1.8 OperationalError:没有这样的列: [英] Django 1.8 OperationalError: no such column:

查看:58
本文介绍了Django 1.8 OperationalError:没有这样的列:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.8,并且在添加到我的models.py时遇到问题。当前是:

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:没有这样的列[添加的元素]

OperationalError: no such column [added element]

例如,我添加了 founder = models.CharField(max_length = 200,blank = True),然后我运行了程序,就得到了

For example I added founder = models.CharField(max_length=200, blank=True) and I ran the program and I got


django.db.utils.OperationalError:没有这样的列:companies_company.founder

django.db.utils.OperationalError: no such column: companies_company.founder


推荐答案

在控制台中运行以下命令:

Run in your console this commands:

manage.py makemigrations app_name
manage.py migrate app_name

每次在应用程序中更改模型时,都应将更改迁移到数据库使用makemigration和migration命令。将新列添加到数据库表时,必须将该列的值添加到所有现有行。您可以通过在模型的新字段中设置默认值来实现。
或在运行迁移命令时设置值(django会自动提出此建议)
您可以在文档

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

这篇关于Django 1.8 OperationalError:没有这样的列:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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