From 69017bade0bcce0671e7d64c5a78626efa82a4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 12 Sep 2015 22:06:35 +0300 Subject: [PATCH] [1.8.x] Fixed #25393 -- Fixed MySQL crash when adding text/blob field with unhashable default. Backport of 4d933ad4181a511f3ced98edba4e17aff054e0e2 from master --- django/db/backends/mysql/schema.py | 3 ++- docs/releases/1.8.5.txt | 3 +++ docs/spelling_wordlist | 1 + tests/schema/tests.py | 12 ++++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py index 91a14ad567..2d35da8c09 100644 --- a/django/db/backends/mysql/schema.py +++ b/django/db/backends/mysql/schema.py @@ -43,7 +43,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), diff --git a/docs/releases/1.8.5.txt b/docs/releases/1.8.5.txt index c2557bd1c2..917599447f 100644 --- a/docs/releases/1.8.5.txt +++ b/docs/releases/1.8.5.txt @@ -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`). diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist index 1bdcc3af66..73618b07b9 100644 --- a/docs/spelling_wordlist +++ b/docs/spelling_wordlist @@ -695,6 +695,7 @@ unescaped ungrouped unhandled unharmful +unhashable unicode uninstall uninstalling diff --git a/tests/schema/tests.py b/tests/schema/tests.py index aa35bfddc9..d4a411ecfe 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -1535,3 +1535,15 @@ class SchemaTests(TransactionTestCase): cursor.execute("SELECT surname FROM schema_author;") item = cursor.fetchall()[0] self.assertEqual(item[0], None if connection.features.interprets_empty_strings_as_nulls else '') + + 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)