﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32276	Adding a unique UUIDField with default to an existing model results in IntegrityErrors.	GaramNick	nobody	"Hi,

When adding a UUIDField to an existing model as per the following code I run into IntegrityErrors because of UUID collisions.

Django 3.1 & PostgreSQL 11.8

Before
{{{
from django.db import models

class Enum(models.Model)
    definition = models.CharField(unique=True, max_length=128)
}}}

After
{{{
from uuid import uuid4
from django.db import models

class Enum(models.Model)
    definition = models.CharField(unique=True, max_length=128)
    uid = models.UUIDField(default=uuid4, editable=False, unique=True)
}}}

Migration

{{{
Applying main.0039_enum_uid...Traceback (most recent call last):
  File ""/home/xxx/.local/share/virtualenvs/xxx--9c6SIiZ/lib/python3.8/site-packages/django/db/backends/utils.py"", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: could not create unique index ""main_enum_uid_key""
DETAIL:  Key (uid)=(cad607b1-6e38-4f99-a5fb-f73c2e3e5ad3) is duplicated.
}}}"	Bug	closed	Database layer (models, ORM)	3.1	Normal	duplicate			Unreviewed	0	0	0	0	0	0
