#32276: Adding a UUIDField (default=uuid4, editable=False, unique=True) to an
existing model results in IntegrityErrors on the migration
-------------------------------------+-------------------------------------
               Reporter:  GaramNick  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32276>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.3f7e870a97ba9f317daf78729f7daa7d%40djangoproject.com.

Reply via email to