[Django]-Django : Duplicate entry in uuid4

3๐Ÿ‘

I think you should to slightly edit your second migration to prevent django from updating your models with duplicate uuid values.

def gen_uuid(apps, schema_editor):
    MyModel = apps.get_model('app', 'user_table')
    for row in MyModel.objects.all():
        while True:
            row.uuid = uuid.uuid4()
            if not MyModel.objects.filter(uuid=row.uuid).exists():
                break

        row.save()

The you should re-run your second migration again and then you should be able to run third migration without any problems.

๐Ÿ‘คNick

Leave a comment