从一对多迁移到多对多:interm。 table 没有 UNIQUE 约束
migrating from one-to-many to many-to-many: interm. table does not have UNIQUE constraint
TL;DR; 在同一迁移中更改完全不同的字段作为引入多对多字段导致中间 table 没有 UNIQUE
约束。
root 挑战。
我需要用多对多替换一对多关系。
循序渐进。
假设最初我有:
class Test(models.Model):
pass
class Test2(models.Model):
title = models.CharField(max_length=100)
test = models.ForeignKey(Test)
将test2.test改成ManyToMany
后
class Test(models.Model):
pass
class Test2(models.Model):
title = models.CharField(max_length=100)
tests = models.ManyToManyField(Test)
和 运行 makemigrations
/migrate
我得到新的 table test2_test
对 test_id
+ test2_id
这绝对是我所期待的。
但是有一次我也想更新另一个字段:
class Test(models.Model):
pass
class Test2(models.Model):
title = models.TextField()
tests = models.ManyToManyField(Test)
并且 test2_test
对于 test_id
+ test2_id
没有得到 UNIQUE INDEX
。
作为解决方法,我终于将不同字段的更改移到了单独的迁移中。
问题
有人知道这种行为的原因吗?
实际上它已经 reported to django team 这是已知的错误,并且已经在即将发布的 v.2 版本中得到修复。
在此之前,应使用解决方法,将 ManyToMany 字段与任何其他模型的更改分开引入。
TL;DR; 在同一迁移中更改完全不同的字段作为引入多对多字段导致中间 table 没有 UNIQUE
约束。
root 挑战。
我需要用多对多替换一对多关系。
循序渐进。
假设最初我有:
class Test(models.Model):
pass
class Test2(models.Model):
title = models.CharField(max_length=100)
test = models.ForeignKey(Test)
将test2.test改成ManyToMany
后
class Test(models.Model):
pass
class Test2(models.Model):
title = models.CharField(max_length=100)
tests = models.ManyToManyField(Test)
和 运行 makemigrations
/migrate
我得到新的 table test2_test
对 test_id
+ test2_id
这绝对是我所期待的。
但是有一次我也想更新另一个字段:
class Test(models.Model):
pass
class Test2(models.Model):
title = models.TextField()
tests = models.ManyToManyField(Test)
并且 test2_test
对于 test_id
+ test2_id
没有得到 UNIQUE INDEX
。
作为解决方法,我终于将不同字段的更改移到了单独的迁移中。
问题
有人知道这种行为的原因吗?
实际上它已经 reported to django team 这是已知的错误,并且已经在即将发布的 v.2 版本中得到修复。
在此之前,应使用解决方法,将 ManyToMany 字段与任何其他模型的更改分开引入。