From 7f53a9f26f214bc251ec31613fb406bdd1719041 Mon Sep 17 00:00:00 2001 From: Seth Wang Date: Mon, 29 Jul 2024 12:31:32 +0800 Subject: [PATCH] fix(migration): when there are duplicated keys, the last unique constraint will fail --- .../migrations/0003_remove_user.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rest_framework_idempotency_key/migrations/0003_remove_user.py b/rest_framework_idempotency_key/migrations/0003_remove_user.py index 7e4253b..52641d8 100644 --- a/rest_framework_idempotency_key/migrations/0003_remove_user.py +++ b/rest_framework_idempotency_key/migrations/0003_remove_user.py @@ -2,6 +2,14 @@ from django.db import migrations, models +def remove_duplicated_records(apps, schema_editor): + IdempotencyKey = apps.get_model('rest_framework_idempotency_key', 'IdempotencyKey') + existed_keys = set() + for id_key in IdempotencyKey.objects.all(): + if id_key.idempotency_key in existed_keys: + id_key.delete() + else: + existed_keys.add(id_key.idempotency_key) class Migration(migrations.Migration): @@ -10,6 +18,7 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython(remove_duplicated_records, migrations.RunPython.noop), migrations.AlterField( model_name='idempotencykey', name='user',