【发布时间】:2018-01-29 11:22:45
【问题描述】:
我尝试进行新的迁移,但显示错误。我正在使用 django1.9 和 python 2.7。是否有任何解决方案,以便我可以创建新的迁移。我该如何进行迁移?
django.db.utils.IntegrityError: could not create unique index "appointments_userappointments_advance_c37857a7_uniq"
DETAIL: Key (advance, status)=(5000, 3) is duplicated.
这是我的模型:
class UserAppointments(models.Model):
"""
Represent an Appointment entity
"""
STATUS = (
("1", 'Scheduled'),
("2", 'Active'),
("3", 'Completed'),
("4", 'Cancelled')
)
customer = models.ForeignKey(Customer, null=True, blank=True)
staff_user = models.ForeignKey(StaffUser, null=True, blank=True, related_name='staff_user')
service = models.ForeignKey(Service, null=True, blank=True)
advance = models.IntegerField(default=0, null=True, blank=True)
date_time = models.DateTimeField(auto_now_add=True)
creation_date = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(StaffUser, null=True, blank=True, related_name='created_by')
update_date = models.DateTimeField(auto_now=True, null=True, blank=True)
updated_by = models.ForeignKey(StaffUser, null=True, blank=True, related_name='updated_by')
status = models.CharField(max_length=1, default=1, choices=STATUS)
class Meta:
verbose_name_plural = 'Appointments'
ordering = ['date_time']
def __unicode__(self):
return self.date_time.strftime("%Y-%m-%d %H:%M:%S")
还有我的迁移文件:
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('appointments', '0009_auto_20180114_1838'),
]
operations = [
migrations.AlterModelOptions(
name='userappointments',
options={'ordering': ['advance'], 'verbose_name_plural': 'Appointments'},
),
migrations.AlterUniqueTogether(
name='userappointments',
unique_together=set([('advance', 'status')]),
),
]
【问题讨论】:
-
如果您可以尝试删除并重新创建数据库,然后删除所有迁移文件并尝试再次迁移
-
@alessioferri20 这不是解决方案。我有很多变化。
-
还有文件:'0010_auto_20180117_1240'? @zeeshan
-
尝试按照以下步骤操作:stackoverflow.com/a/27650735/9202856
标签: python django python-2.7