#25007 closed Bug (invalid)
UUIDFIeld(default=uuid.uuid4) created with migrations are the same
Reported by: | afg984 | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.8 |
Severity: | Normal | Keywords: | migrations, uuid, UUIDField |
Cc: | afg984@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Steps to reproduce:
- Create model
class Bar(models.Model): pass
- makemigrations & migrate
- Create some Bar()s in the database
>>> Bar.objects.create() <Bar: Bar object> >>> Bar.objects.create() <Bar: Bar object> >>> Bar.objects.create() <Bar: Bar object> >>> Bar.objects.create() <Bar: Bar object>
- Add a UUIDField
class Bar(models.Model): uu = models.UUIDField(default=uuid.uuid4)
- makemigrations & migrate
if I use unique=True
in the previous step, ./manage.py migrate
gives me:
django.db.utils.IntegrityError: UNIQUE constraint failed: foo_bar__new.uu
- The UUIDs are the same
>>> [bar.uu for bar in Bar.objects.all()] [UUID('fae77227-7555-457d-a049-1e62e030ce1c'), UUID('fae77227-7555-457d-a049-1e62e030ce1c'), UUID('fae77227-7555-457d-a049-1e62e030ce1c'), UUID('fae77227-7555-457d-a049-1e62e030ce1c')]
Environment:
>>> platform.python_version() '3.4.3' >>> platform.platform() 'Linux-4.0.5-1-ARCH-x86_64-with-arch' >>> django.__version__ '1.8.2'
Note:
See TracTickets
for help on using tickets.
Please see how to write migrations that add unique fields.
#23408 suggests to add a warning to
makemigrations
for this case.