From 1a29675d76de00d6fad9b366fc66e8b6d541c3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Fri, 16 May 2014 18:11:29 +0200 Subject: [PATCH] [1.7.x] Fixed #22626 -- Allow BinaryField defaults with SQlite. Also fixes a slight issue in sqlite3.schema._remake_table where default values where quoted with "column name" quoting rules. Reference for quoting: http://www.sqlite.org/lang_expr.html Thanks Shai Berger for the review. Refs #22424. Backport of 6aacb4c991 from master --- django/db/backends/sqlite3/schema.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 5b6155be47..0703688599 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -1,3 +1,5 @@ +import codecs + from decimal import Decimal from django.utils import six from django.apps.registry import Apps @@ -28,6 +30,16 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): return '"%s"' % six.text_type(value) elif value is None: return "NULL" + elif isinstance(value, (bytes, bytearray, six.memoryview)): + # Bytes are only allowed for BLOB fields, encoded as string + # literals containing hexadecimal data and preceded by a single "X" + # character: + # value = b'\x01\x02' => value_hex = b'0102' => return X'0102' + value = bytes(value) + hex_encoder = codecs.getencoder('hex_codec') + value_hex, _length = hex_encoder(value) + # Use 'ascii' encoding for b'01' => '01', no need to use force_text here. + return "X'%s'" % value_hex.decode('ascii') else: raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value))) @@ -37,7 +49,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): """ # Work out the new fields dict / mapping body = dict((f.name, f) for f in model._meta.local_fields) - mapping = dict((f.column, f.column) for f in model._meta.local_fields) + # Since mapping might mix column names and default values, + # its values must be already quoted. + mapping = dict((f.column, self.quote_name(f.column)) for f in model._meta.local_fields) # If any of the new or altered fields is introducing a new PK, # remove the old one restore_pk_field = None @@ -62,7 +76,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): del body[old_field.name] del mapping[old_field.column] body[new_field.name] = new_field - mapping[new_field.column] = old_field.column + mapping[new_field.column] = self.quote_name(old_field.column) # Remove any deleted fields for field in delete_fields: del body[field.name] @@ -90,7 +104,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % ( self.quote_name(temp_model._meta.db_table), ', '.join(self.quote_name(x) for x, y in field_maps), - ', '.join(self.quote_name(y) for x, y in field_maps), + ', '.join(y for x, y in field_maps), self.quote_name(model._meta.db_table), )) # Delete the old table (not using self.delete_model to avoid deleting