From fdf4dc6cea0cdfea061f2d72a368adf6a8d24157 Mon Sep 17 00:00:00 2001 From: Andriy Sokolovskiy Date: Sat, 6 Dec 2014 22:43:19 +0200 Subject: [PATCH] [1.7.x] Fixed #23405 -- Fixed makemigrations prompt when adding Text/CharField. A default is no longer required. Backport of d8f3b86a7691c8aa0ec8f5a064ad4c3218250fed from master --- django/db/migrations/autodetector.py | 7 ++-- docs/releases/1.7.2.txt | 3 ++ tests/migrations/test_autodetector.py | 51 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 23ab3797e45..99951cdae50 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -802,8 +802,11 @@ class MigrationAutodetector(object): None, True )) - # You can't just add NOT NULL fields with no default - if not field.null and not field.has_default() and not isinstance(field, models.ManyToManyField): + # You can't just add NOT NULL fields with no default or fields + # which don't allow empty strings as default. + if (not field.null and not field.has_default() and + not isinstance(field, models.ManyToManyField) and + not (field.blank and field.empty_strings_allowed)): field = field.clone() field.default = self.questioner.ask_not_null_addition(field_name, model_name) self.add_operation( diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index 338bfdd54c3..ea086490cfb 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -140,3 +140,6 @@ Bugfixes a user specified ``default``. For example, a ``CharField`` with ``blank=True`` didn't set existing rows to an empty string which resulted in a crash when adding the ``NOT NULL`` constraint (:ticket:`23987`). + +* ``makemigrations`` no longer prompts for a default value when adding + ``TextField()`` or ``CharField()`` without a ``default`` (:ticket:`23405`). diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 5d1749f4a3f..38771f16ac6 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -54,6 +54,16 @@ class AutodetectorTests(TestCase): ("name", models.CharField(max_length=200, default=DeconstructableObject())), ]) author_custom_pk = ModelState("testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))]) + author_with_biography_non_blank = ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("name", models.CharField()), + ("biography", models.TextField()), + ]) + author_with_biography_blank = ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("name", models.CharField(blank=True)), + ("biography", models.TextField(blank=True)), + ]) author_with_book = ModelState("testapp", "Author", [ ("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), @@ -1641,3 +1651,44 @@ class AutodetectorTests(TestCase): self.assertNumberMigrations(changes, 'a', 1) self.assertOperationTypes(changes, 'a', 0, ["CreateModel"]) self.assertMigrationDependencies(changes, 'a', 0, []) + + def test_add_blank_textfield_and_charfield(self): + """ + #23405 - Adding a NOT NULL and blank `CharField` or `TextField` + without default should not prompt for a default. + """ + class CustomQuestioner(MigrationQuestioner): + def ask_not_null_addition(self, field_name, model_name): + raise Exception("Should not have prompted for not null addition") + + before = self.make_project_state([self.author_empty]) + after = self.make_project_state([self.author_with_biography_blank]) + autodetector = MigrationAutodetector(before, after, CustomQuestioner()) + changes = autodetector._detect_changes() + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0) + + def test_add_non_blank_textfield_and_charfield(self): + """ + #23405 - Adding a NOT NULL and non-blank `CharField` or `TextField` + without default should prompt for a default. + """ + class CustomQuestioner(MigrationQuestioner): + def __init__(self): + super(CustomQuestioner, self).__init__() + self.ask_method_call_count = 0 + + def ask_not_null_addition(self, field_name, model_name): + self.ask_method_call_count += 1 + + before = self.make_project_state([self.author_empty]) + after = self.make_project_state([self.author_with_biography_non_blank]) + questioner_instance = CustomQuestioner() + autodetector = MigrationAutodetector(before, after, questioner_instance) + changes = autodetector._detect_changes() + # need to check for questioner call count + self.assertEqual(questioner_instance.ask_method_call_count, 2) + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0)