2014-01-20 10:45:21 +08:00
|
|
|
from django.core import checks
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.validation import BaseDatabaseValidation
|
2016-03-19 19:15:59 +08:00
|
|
|
from django.utils.version import get_docs_version
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseValidation(BaseDatabaseValidation):
|
2016-03-19 19:15:59 +08:00
|
|
|
def check(self, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
issues = super().check(**kwargs)
|
2016-03-19 19:15:59 +08:00
|
|
|
issues.extend(self._check_sql_mode(**kwargs))
|
|
|
|
return issues
|
|
|
|
|
|
|
|
def _check_sql_mode(self, **kwargs):
|
2020-03-03 15:30:37 +08:00
|
|
|
if not (self.connection.sql_mode & {'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES'}):
|
2016-03-19 19:15:59 +08:00
|
|
|
return [checks.Warning(
|
2020-03-19 16:42:04 +08:00
|
|
|
"%s Strict Mode is not set for database connection '%s'"
|
|
|
|
% (self.connection.display_name, self.connection.alias),
|
|
|
|
hint=(
|
|
|
|
"%s's Strict Mode fixes many data integrity problems in "
|
|
|
|
"%s, such as data truncation upon insertion, by "
|
|
|
|
"escalating warnings into errors. It is strongly "
|
|
|
|
"recommended you activate it. See: "
|
|
|
|
"https://docs.djangoproject.com/en/%s/ref/databases/#mysql-sql-mode"
|
|
|
|
% (
|
|
|
|
self.connection.display_name,
|
|
|
|
self.connection.display_name,
|
|
|
|
get_docs_version(),
|
|
|
|
),
|
|
|
|
),
|
2016-03-19 19:15:59 +08:00
|
|
|
id='mysql.W002',
|
|
|
|
)]
|
|
|
|
return []
|
|
|
|
|
2017-05-15 00:34:45 +08:00
|
|
|
def check_field_type(self, field, field_type):
|
2008-12-16 14:43:18 +08:00
|
|
|
"""
|
2012-04-21 11:04:10 +08:00
|
|
|
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.
|
2017-05-23 23:02:40 +08:00
|
|
|
MySQL doesn't support a database index on some data types.
|
2008-12-16 14:43:18 +08:00
|
|
|
"""
|
2017-05-15 00:34:45 +08:00
|
|
|
errors = []
|
2016-12-01 05:10:22 +08:00
|
|
|
if (field_type.startswith('varchar') and field.unique and
|
|
|
|
(field.max_length is None or int(field.max_length) > 255)):
|
|
|
|
errors.append(
|
2020-02-09 00:51:31 +08:00
|
|
|
checks.Warning(
|
|
|
|
'%s may not allow unique CharFields to have a max_length '
|
2020-03-19 16:42:04 +08:00
|
|
|
'> 255.' % self.connection.display_name,
|
2016-12-01 05:10:22 +08:00
|
|
|
obj=field,
|
2020-02-09 00:51:31 +08:00
|
|
|
hint=(
|
|
|
|
'See: https://docs.djangoproject.com/en/%s/ref/'
|
|
|
|
'databases/#mysql-character-fields' % get_docs_version()
|
|
|
|
),
|
|
|
|
id='mysql.W003',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
2016-12-01 05:10:22 +08:00
|
|
|
)
|
2017-05-23 23:02:40 +08:00
|
|
|
|
|
|
|
if field.db_index and field_type.lower() in self.connection._limited_data_types:
|
|
|
|
errors.append(
|
|
|
|
checks.Warning(
|
2019-07-23 19:34:06 +08:00
|
|
|
'%s does not support a database index on %s columns.'
|
|
|
|
% (self.connection.display_name, field_type),
|
2017-05-23 23:02:40 +08:00
|
|
|
hint=(
|
|
|
|
"An index won't be created. Silence this warning if "
|
|
|
|
"you don't care about it."
|
|
|
|
),
|
|
|
|
obj=field,
|
|
|
|
id='fields.W162',
|
|
|
|
)
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|