Commit 4d933ad4 authored by Ville Skyttä's avatar Ville Skyttä Committed by Tim Graham
Browse files

Fixed #25393 -- Fixed MySQL crash when adding text/blob field with unhashable default.

parent b8b776aa
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -50,7 +50,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
        super(DatabaseSchemaEditor, self).add_field(model, field)

        # Simulate the effect of a one-off default.
        if self.skip_default(field) and field.default not in {None, NOT_PROVIDED}:
        # field.default may be unhashable, so a set isn't used for "in" check.
        if self.skip_default(field) and field.default not in (None, NOT_PROVIDED):
            effective_default = self.effective_default(field)
            self.execute('UPDATE %(table)s SET %(column)s = %%s' % {
                'table': self.quote_name(model._meta.db_table),
+3 −0
Original line number Diff line number Diff line
@@ -29,3 +29,6 @@ Bugfixes

* Alphabetized ordering of imports in ``from django.db import migrations,
  models`` statement in newly created migrations (:ticket:`25384`).

* Fixed migrations crash on MySQL when adding a text or a blob field with an
  unhashable default (:ticket:`25393`).
+1 −0
Original line number Diff line number Diff line
@@ -869,6 +869,7 @@ unglamorous
ungrouped
unhandled
unharmful
unhashable
unicode
uninstall
uninstalling
+12 −0
Original line number Diff line number Diff line
@@ -1673,3 +1673,15 @@ class SchemaTests(TransactionTestCase):
            )
            if connection.features.can_introspect_default:
                self.assertIsNone(field.default)

    def test_add_textfield_unhashable_default(self):
        # Create the table
        with connection.schema_editor() as editor:
            editor.create_model(Author)
        # Create a row
        Author.objects.create(name='Anonymous1')
        # Create a field that has an unhashable default
        new_field = TextField(default={})
        new_field.set_attributes_from_name("info")
        with connection.schema_editor() as editor:
            editor.add_field(Author, new_field)