gpt4 book ai didi

python - Django 迁移删除并重新添加相同的约束

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:30 26 4
gpt4 key购买 nike

求 friend ...有人能解释为什么当我将 blank=True 添加到模型字段时,我的 Django 迁移会删除并在我的表列上重新添加完全相同的约束吗?这是我对模型的更改:

# old definition
class CatalogCourse(models.Model):
subjects = models.ManyToManyField(CatalogSubject, related_name="catalog_course_set")

# new definition with `blank=True`
class CatalogCourse(models.Model):
subjects = models.ManyToManyField(CatalogSubject, related_name="catalog_course_set", blank=True)

当我makemigrations 时,我得到了这个迁移:

class Migration(migrations.Migration):

dependencies = [
('homepage', '0005_previous_migration'),
]

operations = [
migrations.AlterField(
model_name='catalogcourse',
name='subjects',
field=models.ManyToManyField(blank=True, related_name='catalog_course_set', to='homepage.CatalogSubject'),
),
]

此迁移的 SQL 很简单:

BEGIN;
--
-- Alter field subjects on catalogcourse
--
ALTER TABLE "homepage_catalogcourse_subjects" DROP CONSTRAINT "homepa_catalogsubject_id_304824f4_fk_homepage_catalogsubject_id";
ALTER TABLE "homepage_catalogcourse_subjects" ADD CONSTRAINT "homepa_catalogsubject_id_304824f4_fk_homepage_catalogsubject_id" FOREIGN KEY ("catalogsubject_id") REFERENCES "homepage_catalogsubject" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "homepage_catalogcourse_subjects" DROP CONSTRAINT "homepage_catalogcourse_id_cc699e39_fk_homepage_catalogcourse_id";
ALTER TABLE "homepage_catalogcourse_subjects" ADD CONSTRAINT "homepage_catalogcourse_id_cc699e39_fk_homepage_catalogcourse_id" FOREIGN KEY ("catalogcourse_id") REFERENCES "homepage_catalogcourse" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;

Django 是否只是为了删除约束并在我们更改字段时重新添加它?我想不出任何需要发生这种情况的原因?是否存在外键约束时无法执行的操作?

最佳答案

如果看起来您遇到了 ticket 25253 中描述的错误.

关于python - Django 迁移删除并重新添加相同的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45149473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com