Django 迁移错误:列不存在 [英] Django Migration Error: Column does not exist

查看:34
本文介绍了Django 迁移错误:列不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3、Django 1.8.5、Postgres

Python 3, Django 1.8.5, Postgres

我有一个运行良好的模型 Sites.我最近尝试添加一个字段,airport_code,并迁移数据.

I have a model Sites that has been working fine. I recently tried to add a field, airport_code, and migrate the data.

class Site(BaseModel):

  objects = SiteManager()

  name = models.CharField(max_length=200, unique=True)
  domain = models.CharField(max_length=200, unique=True)
  weather = models.CharField(max_length=10)
  nearby_sites = models.ManyToManyField('self', symmetrical=False, blank=True)
  users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
  facebook = models.URLField(max_length=200)
  twitter = models.URLField(max_length=200)
  header_override = models.TextField(blank=True)
  email_header_override = models.TextField(blank=True)
  timely_site_tag_id = models.IntegerField()
  timely_featured_tag_id = models.IntegerField()
  timely_domain = models.CharField(max_length=255)
  sitemap_public_id = models.CharField(max_length=255)
  state = models.CharField(max_length=24)
  airport_code = JSONField()

但是,当我运行 makemigrations 时出现错误:

However, when I ran makemigrations I got an error:

django.db.utils.ProgrammingError: 列sites_site.airport_code 不存在第 1 行:..._site"."sitemap_public_id", "sites_site"."state", "sites_sit...

当然,这没有意义,因为当我尝试在迁移中创建该列时,该列显然不存在.

Of course, this doesn't make sense, because the column obviously does not exist when I'm trying to create it within the migration.

我在 Stack Overflow 上看到很多关于这个 bug 的问题都没有得到解答,或者有手动创建迁移文件的解决方案,或者销毁和重建数据库.这不是一个好的解决方案.

I have seen many questions about this bug on Stack Overflow that are unanswered, or have a solution to manually create the migration file, or destroy and rebuild the database. This is not an okay solution.

推荐答案

通过在 settings.py 中从 INSTALLED_APPS 注释掉 django 调试工具栏,我解决了这个错误.我不确定为什么调试工具栏是罪魁祸首,但在我将其注释掉之后,我能够毫无问题地运行 makemigrationsmigrate.

This bug was resolved for me by commenting out the django debug toolbar from INSTALLED_APPS in settings.py. I am not sure why debug toolbar is the culprit, but after I commented it out, I was able to run makemigrations and migrate with no issue.

希望这对某人有所帮助,因为我花了十二个小时试图弄清楚.

Hoping this helps someone, as I spent twelve hours trying to figure it out.

这篇关于Django 迁移错误:列不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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