Opened 5 years ago
Closed 5 years ago
#32276 closed Bug (duplicate)
Adding a unique UUIDField with default to an existing model results in IntegrityErrors.
| Reported by: | GaramNick | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.1 |
| 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
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.
Change History (1)
comment:1 by , 5 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
| Summary: | Adding a UUIDField (default=uuid4, editable=False, unique=True) to an existing model results in IntegrityErrors on the migration → Adding a unique UUIDField with default to an existing model results in IntegrityErrors. |
Note:
See TracTickets
for help on using tickets.
Duplicate of #23932 where the decision was to document the solution.