Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#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:

  1. Create model
    class Bar(models.Model):
        pass
    
  1. makemigrations & migrate
  1. 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>
    
  1. Add a UUIDField
    class Bar(models.Model):
        uu = models.UUIDField(default=uuid.uuid4)
    
  1. 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

  1. 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'

Change History (2)

comment:1 by Tim Graham, 10 years ago

Resolution: invalid
Status: newclosed

Please see how to write migrations that add unique fields.

#23408 suggests to add a warning to makemigrations for this case.

comment:2 by afg984, 10 years ago

sorry for that, should have read it

Note: See TracTickets for help on using tickets.
Back to Top