1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #28371 -- Fixed Cast() with CharField if the max_length argument isn't provided.

Thanks Tim Graham for the review.
This commit is contained in:
Mariusz Felisiak
2017-07-27 19:36:47 +02:00
committed by GitHub
parent 14172cf442
commit b61d5b1991
8 changed files with 25 additions and 0 deletions

View File

@@ -36,6 +36,8 @@ class BaseDatabaseOperations:
# name) to the data type to use for the Cast() function, if different from
# DatabaseWrapper.data_types.
cast_data_types = {}
# CharField data type if the max_length argument isn't provided.
cast_char_field_without_max_length = None
def __init__(self, connection):
self.connection = connection

View File

@@ -24,6 +24,7 @@ class DatabaseOperations(BaseDatabaseOperations):
'PositiveIntegerField': 'unsigned integer',
'PositiveSmallIntegerField': 'unsigned integer',
}
cast_char_field_without_max_length = 'char'
def date_extract_sql(self, lookup_type, field_name):
# http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html

View File

@@ -49,6 +49,9 @@ BEGIN
END;
/"""
# Oracle doesn't support string without precision; use the max string size.
cast_char_field_without_max_length = 'NVARCHAR2(2000)'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_operators['difference'] = 'MINUS'

View File

@@ -5,6 +5,8 @@ from django.db.backends.base.operations import BaseDatabaseOperations
class DatabaseOperations(BaseDatabaseOperations):
cast_char_field_without_max_length = 'varchar'
def unification_cast_sql(self, output_field):
internal_type = output_field.get_internal_type()
if internal_type in ("GenericIPAddressField", "IPAddressField", "TimeField", "UUIDField"):

View File

@@ -14,6 +14,8 @@ from django.utils.duration import duration_string
class DatabaseOperations(BaseDatabaseOperations):
cast_char_field_without_max_length = 'text'
def bulk_batch_size(self, fields, objs):
"""
SQLite has a compile-time default (SQLITE_LIMIT_VARIABLE_NUMBER) of

View File

@@ -1066,6 +1066,11 @@ class CharField(Field):
else:
return []
def cast_db_type(self, connection):
if self.max_length is None:
return connection.ops.cast_char_field_without_max_length
return super().cast_db_type(connection)
def get_internal_type(self):
return "CharField"