简体   繁体   English

如何修复表 x 没有名为 y 的列?

[英]How do I fix table x has no column named y?

I edited a model in django and as a result I get the error: "table reserve_time has no column named reservation" for the below models:我在 Django 中编辑了一个模型,结果出现以下错误:“表reserve_time 没有名为reservation 的列”:

from django.db import models
import datetime

class Club(models.Model):
    establishment = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    def __unicode__(self):
        return self.establishment

class Day(models.Model):
    club = models.ForeignKey(Club)
    day = models.DateField('day')
    def __unicode__(self):
        return unicode(self.day)

class Court(models.Model):
    club = models.ForeignKey(Club)
    day = models.ForeignKey(Day)
    court = models.IntegerField(max_length=200)
    def __unicode__(self):
        return unicode(self.court)

class Time(models.Model):
    club = models.ForeignKey(Club)
    day = models.ForeignKey(Day)
    court = models.ForeignKey(Court)
    time = models.TimeField('time')
    reservation = models.CharField(max_length=200)
    def __unicode__(self):
        return unicode(self.time)

I ran python manage.py syncdb and python manage.py runserver after, but I still get the above error.之后我运行了 python manage.py syncdb 和 python manage.py runserver ,但仍然出现上述错误。 Any ideas on how to fix this?有想法该怎么解决这个吗? If I remove the "reservation" field, it works fine.如果我删除“保留”字段,它工作正常。 In the admin, the textbox for "reservation" appears but the error comes up when I try save.在管理员中,出现“预订”文本框,但当我尝试保存时出现错误。

The problem here is that django will not do anything to fix your tables when you make field changes.这里的问题是当您进行字段更改时,django 不会做任何事情来修复您的表格。 All syncdb will do is create tables that do not already exist. syncdb所做的只是创建尚不存在的表。 If you decide to change your schema and add fields, you either need to create them manually in your database, or drop the table and let syncdb recreate it, or start using a project like South to handle database migrations.如果您决定更改架构并添加字段,则需要在数据库中手动创建它们,或者删除表并让syncdb重新创建它,或者开始使用像South这样的项目来处理数据库迁移。

The reason this cannot work is because you would already theoretically have existing records in the table, and there is no sure way to know what you do with all those missing fields.这行不通的原因是理论上您已经在表中拥有现有记录,并且没有确定的方法知道您对所有这些缺失的字段做了什么。 Maybe these fields cannot be null.也许这些字段不能为空。 Maybe they have other constraints that must be satisfied.也许他们还有其他必须满足的约束。 Apparently South can manage this situation.显然南方可以处理这种情况。

Update (Dec. 2019)更新(2019 年 12 月)

It has been over 7 years since I have used django, but it seems that nowadays the database migration functionality has been integrated as a first class concept: https://docs.djangoproject.com/en/3.0/topics/migrations/使用 django 已经有 7 年多了,但现在数据库迁移功能似乎已经被整合为一流的概念: https : //docs.djangoproject.com/en/3.0/topics/migrations/

Syncdb does not alter existing models dbwise. Syncdb 不会改变现有模型 dbwise。 One possibility is to manually edit your db, or delete the model from your db and run syncdb again (shouldn't be done in a productive environment without proper backup).一种可能性是手动编辑您的数据库,或从您的数据库中删除模型并再次运行同步数据库(不应在没有适当备份的生产环境中完成)。

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

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