Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.7.x] Fixed #22424 -- Fixed handling of default values for TextFiel…

…d/BinaryField on MySQL.

Thanks syphar for the review and suggestions.

Backport of 1d3d01b from master
  • Loading branch information...
commit d61b6224b09cb2aa6fb75ccecab246f91553ef7b 1 parent 1a29675
@loic loic authored timgraham committed
View
26 django/db/backends/mysql/schema.py
@@ -1,4 +1,5 @@
from django.db.backends.schema import BaseDatabaseSchemaEditor
+from django.db.models import NOT_PROVIDED
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@@ -28,4 +29,27 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def quote_value(self, value):
# Inner import to allow module to fail to load gracefully
import MySQLdb.converters
- return MySQLdb.escape(value, MySQLdb.converters.conversions)
+
+ if isinstance(value, six.string_types):
+ return '"%s"' % six.text_type(value)
+ else:
+ return MySQLdb.escape(value, MySQLdb.converters.conversions)
+
+ def skip_default(self, field):
+ """
+ MySQL doesn't accept default values for longtext and longblob
+ and implicitly treats these columns as nullable.
+ """
+ return field.db_type(self.connection) in {'longtext', 'longblob'}
+
+ def add_field(self, model, field):
+ 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}:
+ effective_default = self.effective_default(field)
+ self.execute('UPDATE %(table)s SET %(column)s=%(default)s' % {
+ 'table': self.quote_name(model._meta.db_table),
+ 'column': self.quote_name(field.column),
+ 'default': self.quote_value(effective_default),
+ })
View
10 django/db/backends/schema.py
@@ -118,6 +118,7 @@ def column_sql(self, model, field, include_default=False):
null = field.null
# If we were told to include a default value, do so
default_value = self.effective_default(field)
+ include_default = include_default and not self.skip_default(field)
if include_default and default_value is not None:
if self.connection.features.requires_literal_defaults:
# Some databases can't take defaults as a parameter (oracle)
@@ -148,6 +149,13 @@ def column_sql(self, model, field, include_default=False):
# Return the sql
return sql, params
+ def skip_default(self, field):
+ """
+ Some backends don't accept default values for certain columns types
+ (i.e. MySQL longtext and longblob).
+ """
+ return False
+
def prepare_default(self, value):
"""
Only used for backends which have requires_literal_defaults feature
@@ -398,7 +406,7 @@ def add_field(self, model, field):
self.execute(sql, params)
# Drop the default if we need to
# (Django usually does not use in-database defaults)
- if field.default is not None:
+ if not self.skip_default(field) and field.default is not None:
sql = self.sql_alter_column % {
"table": self.quote_name(model._meta.db_table),
"changes": self.sql_alter_column_no_default % {
View
111 tests/migrations/test_operations.py
@@ -1,3 +1,5 @@
+from __future__ import unicode_literals
+
import unittest
try:
@@ -312,6 +314,115 @@ def test_add_field(self):
operation.database_backwards("test_adfl", editor, new_state, project_state)
self.assertColumnNotExists("test_adfl_pony", "height")
+ def test_add_charfield(self):
+ """
+ Tests the AddField operation on TextField.
+ """
+ project_state = self.set_up_test_model("test_adchfl")
+
+ new_apps = project_state.render()
+ Pony = new_apps.get_model("test_adchfl", "Pony")
+ pony = Pony.objects.create(weight=42)
+
+ new_state = self.apply_operations("test_adchfl", project_state, [
+ migrations.AddField(
+ "Pony",
+ "text",
+ models.CharField(max_length=10, default="some text"),
+ ),
+ migrations.AddField(
+ "Pony",
+ "empty",
+ models.CharField(max_length=10, default=""),
+ ),
+ # If not properly quoted digits would be interpreted as an int.
+ migrations.AddField(
+ "Pony",
+ "digits",
+ models.CharField(max_length=10, default="42"),
+ ),
+ ])
+
+ new_apps = new_state.render()
+ Pony = new_apps.get_model("test_adchfl", "Pony")
+ pony = Pony.objects.get(pk=pony.pk)
+ self.assertEqual(pony.text, "some text")
+ self.assertEqual(pony.empty, "")
+ self.assertEqual(pony.digits, "42")
+
+ def test_add_textfield(self):
+ """
+ Tests the AddField operation on TextField.
+ """
+ project_state = self.set_up_test_model("test_adtxtfl")
+
+ new_apps = project_state.render()
+ Pony = new_apps.get_model("test_adtxtfl", "Pony")
+ pony = Pony.objects.create(weight=42)
+
+ new_state = self.apply_operations("test_adtxtfl", project_state, [
+ migrations.AddField(
+ "Pony",
+ "text",
+ models.TextField(default="some text"),
+ ),
+ migrations.AddField(
+ "Pony",
+ "empty",
+ models.TextField(default=""),
+ ),
+ # If not properly quoted digits would be interpreted as an int.
+ migrations.AddField(
+ "Pony",
+ "digits",
+ models.TextField(default="42"),
+ ),
+ ])
+
+ new_apps = new_state.render()
+ Pony = new_apps.get_model("test_adtxtfl", "Pony")
+ pony = Pony.objects.get(pk=pony.pk)
+ self.assertEqual(pony.text, "some text")
+ self.assertEqual(pony.empty, "")
+ self.assertEqual(pony.digits, "42")
+
+ def test_add_binaryfield(self):
+ """
+ Tests the AddField operation on TextField/BinaryField.
+ """
+ project_state = self.set_up_test_model("test_adbinfl")
+
+ new_apps = project_state.render()
+ Pony = new_apps.get_model("test_adbinfl", "Pony")
+ pony = Pony.objects.create(weight=42)
+
+ new_state = self.apply_operations("test_adbinfl", project_state, [
+ migrations.AddField(
+ "Pony",
+ "blob",
+ models.BinaryField(default=b"some text"),
+ ),
+ migrations.AddField(
+ "Pony",
+ "empty",
+ models.BinaryField(default=b""),
+ ),
+ # If not properly quoted digits would be interpreted as an int.
+ migrations.AddField(
+ "Pony",
+ "digits",
+ models.BinaryField(default=b"42"),
+ ),
+ ])
+
+ new_apps = new_state.render()
+ Pony = new_apps.get_model("test_adbinfl", "Pony")
+ pony = Pony.objects.get(pk=pony.pk)
+ # SQLite returns buffer/memoryview, cast to bytes for checking.
+ self.assertEqual(bytes(pony.blob), b"some text")
+ self.assertEqual(bytes(pony.empty), b"")
+ self.assertEqual(bytes(pony.digits), b"42")
+
def test_column_name_quoting(self):
"""
Column names that are SQL keywords shouldn't cause problems when used
Please sign in to comment.
Something went wrong with that request. Please try again.