Find file
Fetching contributors…
Cannot retrieve contributors at this time
65 lines (55 sloc) 2.5 KB
from django.core import checks
from django.db.backends.base.validation import BaseDatabaseValidation
from django.utils.version import get_docs_version
class DatabaseValidation(BaseDatabaseValidation):
def check(self, **kwargs):
issues = super().check(**kwargs)
return issues
def _check_sql_mode(self, **kwargs):
with self.connection.cursor() as cursor:
cursor.execute("SELECT @@sql_mode")
sql_mode = cursor.fetchone()
modes = set(sql_mode[0].split(',') if sql_mode else ())
return [checks.Warning(
"MySQL Strict Mode is not set for database connection '%s'" % self.connection.alias,
hint="MySQL's Strict Mode fixes many data integrity problems in MySQL, "
"such as data truncation upon insertion, by escalating warnings into "
"errors. It is strongly recommended you activate it. See: "
% (get_docs_version(),),
return []
def check_field(self, field, **kwargs):
MySQL has the following field length restriction:
No character (varchar) fields can have a length exceeding 255
characters if they have a unique index on them.
errors = super().check_field(field, **kwargs)
# Ignore any related fields.
if getattr(field, 'remote_field', None):
return errors
# Ignore fields with unsupported features.
db_supports_all_required_features = all(
getattr(self.connection.features, feature, False)
for feature in field.model._meta.required_db_features
if not db_supports_all_required_features:
return errors
field_type = field.db_type(self.connection)
# Ignore non-concrete fields.
if field_type is None:
return errors
if (field_type.startswith('varchar') and field.unique and
(field.max_length is None or int(field.max_length) > 255)):
'MySQL does not allow unique CharFields to have a max_length > 255.',
return errors