1
0
mirror of https://github.com/django/django.git synced 2025-10-29 16:46:11 +00:00

Fixed #22424 -- Fixed handling of default values for TextField/BinaryField on MySQL.

Thanks syphar for the review and suggestions.
This commit is contained in:
Loic Bistuer
2014-05-18 11:58:16 +07:00
parent 6aacb4c991
commit 1d3d01b4f7
3 changed files with 146 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
from django.db.backends.schema import BaseDatabaseSchemaEditor
from django.db.models import NOT_PROVIDED
from django.utils import six
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@@ -28,4 +30,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),
})