我正在使用Python 3.7,Django和PostGres 9.5。我最近编辑了我的模型,以删除一些列并添加其他列...
YourTable
我进行了迁移,一切似乎都很顺利...
class MainSiteStatByHour(models.Model):
total_score = models.DecimalField(default=0, max_digits=12, decimal_places=2)
num_articles = models.IntegerField(default=0)
hour_of_day = IntegerField(
null=True,
validators=[
MaxValueValidator(23),
MinValueValidator(0)
]
)
index = models.FloatField(default=0)
...
class Meta:
unique_together = ("hour_of_day",)
迁移看起来像这样:
(venv) localhost:mainsite_project davea$ python manage.py makemigrations mainsite
Did you rename mainsitestatbyhour.elapsed_time_in_seconds to mainsitestatbyhour.index (a FloatField)? [y/N] N
Migrations for 'mainsite':
mainsite/migrations/0019_auto_20190401_2139.py
- Add field index to mainsitestatbyhour
- Remove field elapsed_time_in_seconds from mainsitestatbyhour
- Remove field website from mainsitestatbyhour
- Alter unique_together for mainsitestatbyhour (1 constraint(s))
但是当我运行迁移时,我收到一个错误,抱怨我删除的字段之一不存在...
# Generated by Django 2.1.4 on 2019-04-01 21:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mainsite', '0018_auto_20190401_1713'),
]
operations = [
migrations.AddField(
model_name='mainsitestatbyhour',
name='index',
field=models.FloatField(default=0),
),
migrations.RemoveField(
model_name='mainsitestatbyhour',
name='elapsed_time_in_seconds',
),
migrations.RemoveField(
model_name='mainsitestatbyhour',
name='website',
),
migrations.AlterUniqueTogether(
name='mainsitestatbyhour',
unique_together={('hour_of_day',)},
),
]
我需要对自动生成的migratino做什么以解决此问题?
答案 0 :(得分:0)
unique_together约束的website
字段部分是否存在?如果是这样,则迁移过程可能希望将其从约束中删除并由于尚未删除该字段而失败。
尝试将迁移文件的AlterUniqueTogether
列表中的Migration.operations
向上移动:
operations = [
...
migrations.AlterUniqueTogether(
name='mainsitestatbyhour',
unique_together={('hour_of_day',)},
),
migrations.RemoveField(
model_name='mainsitestatbyhour',
name='website',
),
]