Skip to content

Commit

Permalink
fix(migration): when there are duplicated keys, the last unique const…
Browse files Browse the repository at this point in the history
…raint will fail
  • Loading branch information
pkyosx committed Jul 29, 2024
1 parent 77f04b4 commit 7f53a9f
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions rest_framework_idempotency_key/migrations/0003_remove_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -10,6 +18,7 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(remove_duplicated_records, migrations.RunPython.noop),
migrations.AlterField(
model_name='idempotencykey',
name='user',
Expand Down

0 comments on commit 7f53a9f

Please sign in to comment.