Django模型改变重复列

时间:2015-09-15 10:40:32

标签: django postgresql

我有两个具有外键关系的模型类如下:问题是错误的我使用现有列名'type'定义了外键关系。如何删除或更改外键列'type'

class LandmarkType(TimeStampedModel):
AIRPORT = 'AIRPORT'
RAILWAYSTATION = 'RAILWAYSTATION'
BUSSTOP = 'BUSSTOP'
OTHER = 'OTHER'
LANDMARK_TYPE_CHOICES = (
    (AIRPORT, 'Airport'),
    (RAILWAYSTATION, 'RailwayStation'),
    (BUSSTOP, 'BusStop'),
    (OTHER, 'Other'),
)
name = models.CharField(max_length=20,
                        choices=LANDMARK_TYPE_CHOICES,
                        default=OTHER)
def __str__(self):  # __unicode__ on Python 2
    return self.name

class Landmark(TimeStampedModel):
type = models.CharField(max_length=200, null=True, blank=True, default='')
name = models.CharField(max_length=200)
description = models.TextField(max_length=2000, null=True, blank=True)
city = models.ForeignKey(City)
latitude = models.DecimalField(max_digits=20, decimal_places=10,      null=True, blank=True, default='12.9667')
longitude = models.DecimalField(max_digits=20, decimal_places=10, null=True, blank=True, default='77.5667')
DISPLAY = const.DISPLAY
HIDE = const.HIDE
DISPLAY_CHOICES = const.DISPLAY_CHOICES
status = models.IntegerField(default=HIDE, choices=DISPLAY_CHOICES)
type = models.ForeignKey(LandmarkType, default=4)
popularity = models.PositiveIntegerField(default=0)

2 个答案:

答案 0 :(得分:0)

使用postgresql commandline client并执行ALTER TABLE

ALTER TABLE Landmark DROP CONSTRAINT "type";

答案 1 :(得分:0)

删除重复字段,然后重新运行makemigrations,然后重新运行migrate

根据您拥有的地标数量,您可以编写迁移以重新填充新的type字段,如果没有很多,则可以手动执行。