#28299: Adding a unique UUIDField to an existing model immediately fails with a
database constraint error
-----------------------------------------+------------------------
               Reporter:  Tom            |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Migrations     |        Version:  master
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 If you want to add a unique UUIDField to an existing model, you would add
 the following field definition (as per the docs):

 `column = models.UUIDField(unique=True, default=uuid.uuid4)`

 However, upon creating and running a migration Django calls the `default`
 function *once*, then attempts to update all rows in a single query, which
 obviously violates the unique constraint and throws a database error.

 This is particularly annoying and possibly confusing to new users (the
 error message is not exactly descriptive), and there does not seem to be
 an easy way of overriding it without creating your own custom RunPython
 migrations that sequentially calls the default function for each row.

 Because it's pretty common to have a unique `UUIDField`, and because it's
 a problem you immediately run into when adding it to an existing model,
 could a workaround be added in the migrations framework to be a bit
 smarter about this? Or perhaps in a more generic sense, if `unique=True`
 then re-evaluate the `default` for each row rather than once during an
 `UPDATE` statement.

--
Ticket URL: <https://code.djangoproject.com/ticket/28299>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/046.856bcba72c646a7e91c076a5a737737d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to