Opened 13 months ago
Closed 13 months ago
#34890 closed Uncategorized (invalid)
Migration can produce duplicate entries of UUID4 values for unique fields
Reported by: | Pavlo Pohorieltsev | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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?
Hi there,
Unfortunately you haven't supplied enough information to confirm this is a bug with Django. You've mentioned this is from adding a new field yet there's data violating the unique constraint there. You may need to work through the issue further to isolate the source of the conflict. If you need help you can jump on the forum or Discord to request help from a friendly community member: https://www.djangoproject.com/community/
(Additionally you will also need to confirm this is an issue with the latest version of Django before continuing – Django 3.2 is only receiving security patches)
If you do confirm there is an issue please feel free to reopen this ticket.
Good luck!