简体   繁体   English

Django迁移:django.db.utils.OperationalError:没有这样的列:

[英]Django Migration: django.db.utils.OperationalError: no such column:

Whenever I add a new column to my model I get: 每当我在模型中添加新列时,我都会得到:

django.db.utils.OperationalError: no such column: companies_company.[name of added column] django.db.utils.OperationalError:否这样的列:companies_company。[添加的列的名称]

For example, I just added "employee_count" and whenever I run 例如,我刚添加了“ employee_count”,每次运行

python manage.py makemigrations

I get: 我得到:

django.db.utils.OperationalError: no such column: companies_companyadmin.employee_count django.db.utils.OperationalError:没有这样的列:companies_companyadmin.employee_count

My model.py looks like this: 我的model.py看起来像这样:

from django.db import models

import csv

class Company(models.Model):
   name = models.CharField(max_length=70, blank=True)
   description = models.CharField(max_length=1200, blank=True)
   employee_count = models.IntegerField(blank=True)


class CompanyAdmin(models.Model):
    name = models.CharField(max_length=70, blank=True)
    description = models.CharField(max_length=1200, blank=True)
    employee_count = models.(blank=True)

with open("organizationTest.txt","rU") as f:
        reader = csv.reader(f)
        for row in reader:
            _, created = Company.objects.get_or_create(
                name=row[3].decode('latin-1').encode('utf8'),
                description=row[15].decode('latin-1').encode('utf8'),
                )

The CSV part is simply to populate my DB (I should probably do it somewhere else). CSV部分只是填充我的数据库(我可能应该在其他地方做)。

And my admin.py: 和我的admin.py:

from django.contrib import admin
from .models import Company


class CompanyAdmin(admin.ModelAdmin):
    pass


admin.site.register(Company, CompanyAdmin)

This also happens when I try to run the server or "syncdb". 当我尝试运行服务器或“ syncdb”时,也会发生这种情况。

If anyone knows why this is happening and how I could fix this I would love to know! 如果有人知道为什么会这样以及我如何解决这个问题,我很想知道! Thanks in advance! 提前致谢! I'm happy to answer any questions you may have. 我很高兴回答您可能遇到的任何问题。

In my case I had an initial value in some Querysets of an updated model. 就我而言,我在更新模型的某些查询集中具有一个初始值。 When I removed these initial statements, makemigrations had worked. 当我删除这些初始声明时,makemigrations起作用了。

Appears my original answer wasn't clear enough. 似乎我的原始答案不够清楚。

Company.objects.get_or_create( name=row[3].decode('latin-1').encode('utf8'), description=row[15].decode('latin-1').encode('utf8'), )

This block is out of a class so it runs when the server is started or basically everytime manage.py is run. 此块不在类之内,因此它在服务器启动时或基本上在每次manage.py运行时运行。 Company.objects.get_or_create(...) is trying to run, on the updated database model that can't be created yet. Company.objects.get_or_create(...)尝试在尚未创建的更新数据库模型上运行。 To make the migrations you have to comment this line out, then make the migrations, then it will work. 要进行迁移,您必须先注释掉这一行,然后进行迁移,然后它才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM