From f6a556b806dd896a47c8853dd3cc4028fc8861fe Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 4 Mar 2022 10:40:29 +0900 Subject: [PATCH] Update migration when adding uuids reference: https://docs.djangoproject.com/en/dev/howto/writing-migrations/#migrations-that-add-unique-fields --- backend/api/migrations/0017_example_uuid.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/api/migrations/0017_example_uuid.py b/backend/api/migrations/0017_example_uuid.py index 3fd2decb..2e5ed6e4 100644 --- a/backend/api/migrations/0017_example_uuid.py +++ b/backend/api/migrations/0017_example_uuid.py @@ -9,7 +9,7 @@ def create_uuid(apps, schema_editor): Example = apps.get_model("api", "example") for example in Example.objects.all(): example.uuid = uuid.uuid4() - example.save() + example.save(update_fields=["uuid"]) class Migration(migrations.Migration): @@ -24,6 +24,8 @@ class Migration(migrations.Migration): name="uuid", field=models.UUIDField(editable=False, blank=True, null=True), ), - migrations.RunPython(create_uuid), - migrations.AlterField(model_name="example", name="uuid", field=models.UUIDField(db_index=True, unique=True)), + migrations.RunPython(create_uuid, reverse_code=migrations.RunPython.noop), + migrations.AlterField( + model_name="example", name="uuid", field=models.UUIDField(default=uuid.uuid4, db_index=True, unique=True) + ), ]