#33108: Makemigrations allows on-off default when adding non-nullable unique 
field
-------------------------------------+-------------------------------------
               Reporter:  Julian-    |          Owner:  nobody
  Samuel Gebühr                      |
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  3.2
  Migrations                         |
               Severity:  Normal     |       Keywords:  migrations
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 == Reproduce

 Create a model like

 {{{
 class Book(models.Model):
     title = models.CharField(max_length=200)
 }}}

 and apply with ''makemigrations'' and ''migrate'', then add a book to the
 database. Now extend the book model by a unique, non-nullable field (e.g.)
 ISBN

 {{{
 class Book(models.Model):
     title = models.CharField(max_length=200)
     isbn = models.CharField('ISBN', max_length=13, unique=True,
 help_text='ISBN number (13 Characters)')
 }}}

 To apply these changes you try ''makemigrations'' which tells you


 {{{
 You are trying to add a non-nullable field 'isbn' to book without a
 default; we can't do that (the database needs som
 ething to populate existing rows).
 Please select a fix:
  1) Provide a one-off default now (will be set on all existing rows with a
 null value for this column)
  2) Quit, and let me add a default in models.py
 }}}
 Choosing 1) and using a default value let's you finish but then
 ''migrate'' fails with
 {{{

 django.db.utils.IntegrityError: (1062, "Duplicate entry '1234567890123'
 for key 'isbn'")
 }}}

 == Expected Behaviour

 I would expect ''makemigrations'' to say something in the line of

 {{{
 You can't add make a field unique when not all entries in the database
 have a unique value for this field. A solution might be to remove the
 unique constraint and add a unique value for this field to all entries.
 Then re-add the unique constraint.
 }}}

 and fail to make the migration.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33108>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.ff3655e297342e0c9e5ea8b2505b002b%40djangoproject.com.

Reply via email to