#34890: Migration can produce duplicate entries of UUID4 values for unique 
fields
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  makisukurisu                       |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  3.2
  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          |
-------------------------------------+-------------------------------------
 Hello, I've run into the following problem.

 When adding a new field, with UUID-4 generator (if I can say so),
 migration fails on a larger dataset of my testing environment.
 I get this error:
 {{{
 Running migrations:
   Applying ***_module.***_migration_name...Traceback (most recent call
 last):
   File "***/python/lib/python3.10/site-
 packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "***/python/lib/python3.10/site-
 packages/django/db/backends/mysql/base.py", line 73, in execute
     return self.cursor.execute(query, args)
   File "***/python/lib/python3.10/site-packages/MySQLdb/cursors.py", line
 206, in execute
     res = self._query(query)
   File "***/python/lib/python3.10/site-packages/MySQLdb/cursors.py", line
 319, in _query
     db.query(q)
   File "***/python/lib/python3.10/site-packages/MySQLdb/connections.py",
 line 254, in query
     _mysql.connection.query(self, query)
 MySQLdb.IntegrityError: (1062, "Duplicate entry
 'd278f389a95b47b2aaea6fdb5e14d6a4' for key 'external_id'")
 }}}

 The field itself looks like:
 {{{
 class SomeModel(BaseModel):
     external_id = models.UUIDField(default=uuid.uuid4, editable=False,
 unique=True)
     #...
 }}}

 And finally, migration code looks like this:
 {{{
 # Generated by Django 3.2 on 2023-10-04 08:37

 from django.db import migrations, models
 import uuid


 class Migration(migrations.Migration):

     dependencies = [
         ('module_name', 'migration_name_and_number'),
     ]

     operations = [
         migrations.AddField(
             model_name='SomeModel',
             name='external_id',
             field=models.UUIDField(default=uuid.uuid4, editable=False,
 unique=True),
         ),
     ]
 }}}

 Since this is a new field, there's no possibility of collision to happen
 with previous values. This leads me to conclude, there's a problem with
 either concurrent requests, that result in Integrity error, or some other
 error in code that applies migrations.

 As you can see – I use MySQL database backend, and Python3.10.
 In local this migration is applied just fine, because there's way smaller
 dataset (we are comparing 2800 and 10 records in the database).

 Please advise, if there's some known fix for the issue, or a possible
 workaround. Thank you!
 If you need some additional information, please let me know, I'll gladly
 provide it.
 ''P.S. I've noticed that the value shown in the exception is not exactly a
 UUID-4 string, is this expected?''

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34890>
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/0107018afea77ca0-a7fbc751-a495-45b2-99e0-4ea97118ba90-000000%40eu-central-1.amazonses.com.

Reply via email to