creation.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from django.db.backends.creation import BaseDatabaseCreation
  2. class DatabaseCreation(BaseDatabaseCreation):
  3. # This dictionary maps Field objects to their associated MySQL column
  4. # types, as strings. Column-type strings can contain format strings; they'll
  5. # be interpolated against the values of Field.__dict__ before being output.
  6. # If a column type is set to None, it won't be included in the output.
  7. data_types = {
  8. 'AutoField': 'integer AUTO_INCREMENT',
  9. 'BinaryField': 'longblob',
  10. 'BooleanField': 'bool',
  11. 'CharField': 'varchar(%(max_length)s)',
  12. 'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
  13. 'DateField': 'date',
  14. 'DateTimeField': 'datetime',
  15. 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
  16. 'FileField': 'varchar(%(max_length)s)',
  17. 'FilePathField': 'varchar(%(max_length)s)',
  18. 'FloatField': 'double precision',
  19. 'IntegerField': 'integer',
  20. 'BigIntegerField': 'bigint',
  21. 'IPAddressField': 'char(15)',
  22. 'GenericIPAddressField': 'char(39)',
  23. 'NullBooleanField': 'bool',
  24. 'OneToOneField': 'integer',
  25. 'PositiveIntegerField': 'integer UNSIGNED',
  26. 'PositiveSmallIntegerField': 'smallint UNSIGNED',
  27. 'SlugField': 'varchar(%(max_length)s)',
  28. 'SmallIntegerField': 'smallint',
  29. 'TextField': 'longtext',
  30. 'TimeField': 'time',
  31. }
  32. def sql_table_creation_suffix(self):
  33. suffix = []
  34. test_settings = self.connection.settings_dict['TEST']
  35. if test_settings['CHARSET']:
  36. suffix.append('CHARACTER SET %s' % test_settings['CHARSET'])
  37. if test_settings['COLLATION']:
  38. suffix.append('COLLATE %s' % test_settings['COLLATION'])
  39. return ' '.join(suffix)
  40. def sql_for_inline_foreign_key_references(self, model, field, known_models, style):
  41. "All inline references are pending under MySQL"
  42. return [], True
  43. def sql_destroy_indexes_for_fields(self, model, fields, style):
  44. if len(fields) == 1 and fields[0].db_tablespace:
  45. tablespace_sql = self.connection.ops.tablespace_sql(fields[0].db_tablespace)
  46. elif model._meta.db_tablespace:
  47. tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace)
  48. else:
  49. tablespace_sql = ""
  50. if tablespace_sql:
  51. tablespace_sql = " " + tablespace_sql
  52. field_names = []
  53. qn = self.connection.ops.quote_name
  54. for f in fields:
  55. field_names.append(style.SQL_FIELD(qn(f.column)))
  56. index_name = "%s_%s" % (model._meta.db_table, self._digest([f.name for f in fields]))
  57. from ..utils import truncate_name
  58. return [
  59. style.SQL_KEYWORD("DROP INDEX") + " " +
  60. style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + " " +
  61. style.SQL_KEYWORD("ON") + " " +
  62. style.SQL_TABLE(qn(model._meta.db_table)) + ";",
  63. ]