我要删除的数据库中有两个字段的重复项。
我可以通过简单地在对上设置unique_together
并进行迁移来做到这一点吗?我正在使用Django 2.0。这是模型(已添加unique_together
):
class Candle(models.Model):
symbol = models.CharField(max_length=32)
name = models.CharField(max_length=32)
timestamp = models.DateTimeField()
open = models.FloatField()
high = models.FloatField()
low = models.FloatField()
close = models.FloatField()
volume = models.FloatField()
def __str__(self):
return 'Candlestick for {} at {}.'.format(self.symbol,
self.timestamp)
class Meta:
indexes = [
models.Index(fields=['symbol']),
models.Index(fields=['timestamp'])
]
unique_together = (('symbol', 'timestamp'),)
否则,删除这些重复项并避免插入新重复项的最佳方法是什么?我应该直接进入PostGreSQL,删除重复项,然后执行规则吗?
我发现了以下两种可能的解决方案来删除重复项: