【问题标题】:How to remove unique constraint using South?如何使用 South 删除唯一约束?
【发布时间】:2023-04-06 07:20:01
【问题描述】:

我正在尝试删除在两个模型字段中定义的 unique 约束(使用 South 0.8.2)。

我需要从:

billing_id = models.PositiveIntegerField(_("Billing id"), unique=True, blank=True, null=True)
tax_code = models.CharField(_("Tax code"), max_length=10, unique=True, blank=True, null=True)

收件人:

billing_id = models.PositiveIntegerField(_("Billing id"), blank=True, null=True)
tax_code = models.CharField(_("Tax code"), max_length=10, blank=True, default='')

但是,当我运行迁移时,我收到以下错误:

Warning: Data truncated for column 'tax_code' at row 1

如果我再次运行它,我会得到:

ValueError: Cannot find a UNIQUE constraint on table startups_startup, columns ['billing_id']

到了这一步,我不得不手动重新创建唯一索引。

表上有现有记录,我正在使用 MySQL。

有没有“简单的方法”来做到这一点?

【问题讨论】:

    标签: mysql django django-south


    【解决方案1】:

    为了解决这个问题,我做了以下事情:

    1. 移除唯一约束,运行schemamigration 并迁移它。
    2. tax_code 中删除 null=True,运行 schemamigration 并再次迁移。

    第 2 步)显示了 Truncated data 警告,但运行良好。重新迁移它不会显示任何错误,所以一切看起来都很好!

    希望这对其他人也有帮助!

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2011-07-26
      • 2012-07-26
      相关资源
      最近更新 更多