[Django] #33108: Makemigrations allows on-off default when adding non-nullable unique field

14 views
Skip to first unread message

Django

unread,
Sep 13, 2021, 7:36:40 PM9/13/21
to django-...@googlegroups.com
#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.

Django

unread,
Sep 14, 2021, 12:37:31 AM9/14/21
to django-...@googlegroups.com
#33108: Makemigrations allows on-off default when adding non-nullable unique field
-------------------------------------+-------------------------------------
Reporter: Julian-Samuel | Owner: nobody
Gebühr |
Type: | Status: closed
Cleanup/optimization |
Component: Migrations | Version: 3.2
Severity: Normal | Resolution: wontfix
Keywords: migrations | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Bug => Cleanup/optimization
* resolution: => wontfix


Comment:

Thanks for the report.

> ... and fail to make the migration.

This is not an expected behavior, because you can add a column to an empty
table or a table with a single row. In both cases it works fine. Moreover
we already inform users that provided value ''"...will be set on all
existing rows with a null value for this column"''. Also, it's
[https://docs.djangoproject.com/en/stable/howto/writing-migrations
/#migrations-that-add-unique-fields documented] in details how to deal
with such migration.

--
Ticket URL: <https://code.djangoproject.com/ticket/33108#comment:1>

Reply all
Reply to author
Forward
0 new messages