【问题标题】:Django Migration is not apply new migrationsDjango Migration 不应用新的迁移
【发布时间】: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


【解决方案1】:

看起来这里的问题是这是一个唯一的共同索引。因此,您的数据库中至少有 2 行共享相同的字段值。因此,由于违反了唯一性保证,它不能很大的复合唯一索引。

您不妨考虑使用常规复合索引。

请参阅这些文档以获取示例:django indexes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2015-08-02
    • 2022-11-25
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    相关资源
    最近更新 更多