Ticket #23405: ticket_23405_for_1_7.patch

File ticket_23405_for_1_7.patch, 4.8 KB (added by Andriy Sokolovskiy, 10 years ago)

Tim Graham: Could you also provide a patch in the comments here that I could apply on top of this one when backporting to 1.7 that replaces the usage of mock since we don't have it available there?

  • django/db/migrations/autodetector.py

    diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
    index 007ea33..60756c9 100644
    a b class MigrationAutodetector(object):  
    794794                        None,
    795795                        True
    796796                    ))
    797             # You can't just add NOT NULL fields with no default
    798             if not field.null and not field.has_default() and not isinstance(field, models.ManyToManyField):
     797            # You can't just add NOT NULL fields with no default or fields,
     798            # which are not allowing empty strings as default.
     799            if (not field.null and not field.has_default()
     800               and not isinstance(field, models.ManyToManyField) and not
     801               (field.blank and field.empty_strings_allowed)):
    799802                field = field.clone()
    800803                field.default = self.questioner.ask_not_null_addition(field_name, model_name)
    801804                self.add_operation(
  • docs/releases/1.7.2.txt

    diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt
    index ad20680..7b340c2 100644
    a b Bugfixes  
    121121  model (:ticket:`23956`).
    122122
    123123* Fixed a crash when a ``MultiValueField`` has invalid data (:ticket:`23674`).
     124
     125* ``makemigrations`` no longer prompts for a default value when adding
     126  ``TextField()`` or ``CharField()`` without a default (:ticket:`23405`).
  • tests/migrations/test_autodetector.py

    diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
    index 9e7511c..ede933f 100644
    a b class AutodetectorTests(TestCase):  
    6262        ("name", models.CharField(max_length=200, default=models.IntegerField())),
    6363    ])
    6464    author_custom_pk = ModelState("testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))])
     65    author_with_biography_non_blank = ModelState("testapp", "Author", [
     66        ("id", models.AutoField(primary_key=True)),
     67        ("name", models.CharField()),
     68        ("biography", models.TextField()),
     69    ])
     70    author_with_biography_blank = ModelState("testapp", "Author", [
     71        ("id", models.AutoField(primary_key=True)),
     72        ("name", models.CharField(blank=True)),
     73        ("biography", models.TextField(blank=True)),
     74    ])
    6575    author_with_book = ModelState("testapp", "Author", [
    6676        ("id", models.AutoField(primary_key=True)),
    6777        ("name", models.CharField(max_length=200)),
    class AutodetectorTests(TestCase):  
    16821692        self.assertNumberMigrations(changes, 'a', 1)
    16831693        self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
    16841694        self.assertMigrationDependencies(changes, 'a', 0, [])
     1695
     1696    def test_add_blank_textfield_and_charfield(self):
     1697        """
     1698        #23405 - Adding a NOT NULL and blank `CharField` or `TextField`
     1699        without default should not prompt for a default.
     1700        """
     1701        class CustomQuestioner(MigrationQuestioner):
     1702            def ask_not_null_addition(self, field_name, model_name):
     1703                raise Exception("Should not have prompted for not null addition")
     1704
     1705        before = self.make_project_state([self.author_empty])
     1706        after = self.make_project_state([self.author_with_biography_blank])
     1707        autodetector = MigrationAutodetector(before, after, CustomQuestioner())
     1708        changes = autodetector._detect_changes()
     1709        self.assertNumberMigrations(changes, 'testapp', 1)
     1710        self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
     1711        self.assertOperationAttributes(changes, 'testapp', 0, 0)
     1712
     1713    def test_add_non_blank_textfield_and_charfield(self):
     1714        """
     1715        #23405 - Adding a NOT NULL and non-blank `CharField` or `TextField`
     1716        without default should prompt for a default.
     1717        """
     1718        class CustomQuestioner(MigrationQuestioner):
     1719            def __init__(self):
     1720                super(CustomQuestioner, self).__init__()
     1721                self.ask_method_call_count = 0
     1722
     1723            def ask_not_null_addition(self, field_name, model_name):
     1724                self.ask_method_call_count += 1
     1725
     1726        before = self.make_project_state([self.author_empty])
     1727        after = self.make_project_state([self.author_with_biography_non_blank])
     1728        questioner_instance = CustomQuestioner()
     1729        autodetector = MigrationAutodetector(before, after, questioner_instance)
     1730        changes = autodetector._detect_changes()
     1731        # need to check for questioner call count
     1732        self.assertEqual(questioner_instance.ask_method_call_count, 2)
     1733        self.assertNumberMigrations(changes, 'testapp', 1)
     1734        self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
     1735        self.assertOperationAttributes(changes, 'testapp', 0, 0)
Back to Top