From 10c40dc591e5005a385b1a35a2613b4d981ec202 Mon Sep 17 00:00:00 2001 From: Roland Szabo Date: Thu, 3 Mar 2022 15:22:28 +0200 Subject: [PATCH 1/3] Fix migration --- backend/api/migrations/0017_example_uuid.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/api/migrations/0017_example_uuid.py b/backend/api/migrations/0017_example_uuid.py index 5d70fbfd..8630284c 100644 --- a/backend/api/migrations/0017_example_uuid.py +++ b/backend/api/migrations/0017_example_uuid.py @@ -5,6 +5,13 @@ import uuid from django.db import migrations, models +def create_uuid(apps, schema_editor): + Example = apps.get_model('api', 'example') + for example in Example.objects.all(): + example.uuid = uuid.uuid4() + example.save() + + class Migration(migrations.Migration): dependencies = [ @@ -15,6 +22,12 @@ class Migration(migrations.Migration): migrations.AddField( model_name="example", name="uuid", - field=models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, unique=True), + 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) + ) + ] \ No newline at end of file From d537640eac7292554e523e756ed23dc514e3e6b9 Mon Sep 17 00:00:00 2001 From: Roland Szabo Date: Thu, 3 Mar 2022 15:32:49 +0200 Subject: [PATCH 2/3] Black formatting --- backend/api/migrations/0017_example_uuid.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/backend/api/migrations/0017_example_uuid.py b/backend/api/migrations/0017_example_uuid.py index 8630284c..3fd2decb 100644 --- a/backend/api/migrations/0017_example_uuid.py +++ b/backend/api/migrations/0017_example_uuid.py @@ -6,7 +6,7 @@ from django.db import migrations, models def create_uuid(apps, schema_editor): - Example = apps.get_model('api', 'example') + Example = apps.get_model("api", "example") for example in Example.objects.all(): example.uuid = uuid.uuid4() example.save() @@ -25,9 +25,5 @@ class Migration(migrations.Migration): 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) - ) - ] \ No newline at end of file + migrations.AlterField(model_name="example", name="uuid", field=models.UUIDField(db_index=True, unique=True)), + ] From f6a556b806dd896a47c8853dd3cc4028fc8861fe Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 4 Mar 2022 10:40:29 +0900 Subject: [PATCH 3/3] 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) + ), ]