首页 > 解决方案 > psycopg2.ProgrammingError:列“...”不存在 - 通过 makemigrations 错误?

问题描述

我将一个项目推到 heroku 上,但似乎无法通过它的 makemigrations 取胜。

所以首先我认为一切都很顺利。推送后,我进行了迁移并创建了超级用户。一切看起来都很好,管理员工作除了一个部分: accounts_userprofileinfo 。一旦我尝试在管理员中单击该表,就会出现此错误:

内部服务器错误:/admin/accounts/userprofileinfo/

回溯(最近一次通话最后):

文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py”,第 65 行,执行 return self.cursor.execute(sql, params)

psycopg2.ProgrammingError:列accounts_userprofileinfo.daca 不存在

第 1 行:选择“accounts_userprofileinfo”。“id”,“accounts_userprofile...

通常你不会在实时服务器上进行迁移,但我认为它可能会修复它,所以我做了。令人惊讶的是,它有一个结果:

Migrations for 'auth':
  .heroku/python/lib/python3.6/site-packages/django/contrib/auth/migrations/0009_auto_20180709_2242.py
    - Alter field email on user

在本地,我的迁移是有结果的:没有变化,但一切正常。有趣的是heroku postgres,无论我多久做一次makemigrations,同样的事情总是会出现auth的迁移,就像它没有保存它一样。不确定这是否会导致我的 account_userprofileinfo 出现服务器错误,但它可能会。

账户模式:

User._meta.get_field('email').__dict__['_unique'] = True

class UserProfileInfo(models.Model):

    TYPE_CHOICES = (
       ('yes', 'yes'),
       ('no', 'no'),
       )
    POSITION_CHOICES = (
        ('President', 'President'),
        ('Vize-President', 'Vize-President'),
        ('Secretary', 'Secretary'),
        ('Member', 'Member'),
    )
    daca = models.CharField(max_length=100, choices=TYPE_CHOICES, default="no")
    committee  = models.CharField(max_length=30, choices=POSITION_CHOICES, default="Member")

    user = models.OneToOneField(User)
    city = models.CharField(max_length=75, blank=False, default='')
    mobile = models.CharField(max_length=16,blank=False, default='')
    experience = models.IntegerField(blank=False, default='0')
    profile_picture = ThumbnailerImageField(upload_to='profile_pics',blank=True, default='/placeholder/neutral3.png',
                                            resize_source=dict(size=(550, 700)))
    joined_date = models.DateTimeField(default=timezone.now, blank=True, null=True,editable=False)

    def __str__(self):
        # Built-in attribute of django.contrib.auth.models.User !
         return self.user.first_name + " " + self.user.last_name

    def create_profile(sender, **kwargs):
        user = kwargs["instance"]
        if kwargs["created"]:
            user_profile = UserProfileInfo(user=user)
            user_profile.save()
            post_save.connect(create_profile, sender=User)

有任何想法吗?谢谢

标签: djangodjango-modelsdjango-migrations

解决方案


同时运行 manage.py migrate。我也不确定你真的需要那个User._meta.get_field('email').__dict__['_unique'] = True。Monkey-patching 默认模型导致在 Django 内部创建新的迁移,我会说这是不好的。要更改默认字段,您最好创建自定义用户模型https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#substituting-a-custom-user-model。但是在已经创建的项目中很难做到。所以也许可以处理表单中的唯一检查。


推荐阅读