|
1 from django.conf import settings |
|
2 from django.db.backends.creation import BaseDatabaseCreation |
|
3 |
|
4 class DatabaseCreation(BaseDatabaseCreation): |
|
5 # This dictionary maps Field objects to their associated MySQL column |
|
6 # types, as strings. Column-type strings can contain format strings; they'll |
|
7 # be interpolated against the values of Field.__dict__ before being output. |
|
8 # If a column type is set to None, it won't be included in the output. |
|
9 data_types = { |
|
10 'AutoField': 'integer AUTO_INCREMENT', |
|
11 'BooleanField': 'bool', |
|
12 'CharField': 'varchar(%(max_length)s)', |
|
13 'CommaSeparatedIntegerField': 'varchar(%(max_length)s)', |
|
14 'DateField': 'date', |
|
15 'DateTimeField': 'datetime', |
|
16 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)', |
|
17 'FileField': 'varchar(%(max_length)s)', |
|
18 'FilePathField': 'varchar(%(max_length)s)', |
|
19 'FloatField': 'double precision', |
|
20 'IntegerField': 'integer', |
|
21 'IPAddressField': 'char(15)', |
|
22 'NullBooleanField': 'bool', |
|
23 'OneToOneField': 'integer', |
|
24 'PositiveIntegerField': 'integer UNSIGNED', |
|
25 'PositiveSmallIntegerField': 'smallint UNSIGNED', |
|
26 'SlugField': 'varchar(%(max_length)s)', |
|
27 'SmallIntegerField': 'smallint', |
|
28 'TextField': 'longtext', |
|
29 'TimeField': 'time', |
|
30 } |
|
31 |
|
32 def sql_table_creation_suffix(self): |
|
33 suffix = [] |
|
34 if settings.TEST_DATABASE_CHARSET: |
|
35 suffix.append('CHARACTER SET %s' % settings.TEST_DATABASE_CHARSET) |
|
36 if settings.TEST_DATABASE_COLLATION: |
|
37 suffix.append('COLLATE %s' % settings.TEST_DATABASE_COLLATION) |
|
38 return ' '.join(suffix) |
|
39 |
|
40 def sql_for_inline_foreign_key_references(self, field, known_models, style): |
|
41 "All inline references are pending under MySQL" |
|
42 return [], True |
|
43 |
|
44 def sql_for_inline_many_to_many_references(self, model, field, style): |
|
45 from django.db import models |
|
46 opts = model._meta |
|
47 qn = self.connection.ops.quote_name |
|
48 |
|
49 table_output = [ |
|
50 ' %s %s %s,' % |
|
51 (style.SQL_FIELD(qn(field.m2m_column_name())), |
|
52 style.SQL_COLTYPE(models.ForeignKey(model).db_type()), |
|
53 style.SQL_KEYWORD('NOT NULL')), |
|
54 ' %s %s %s,' % |
|
55 (style.SQL_FIELD(qn(field.m2m_reverse_name())), |
|
56 style.SQL_COLTYPE(models.ForeignKey(field.rel.to).db_type()), |
|
57 style.SQL_KEYWORD('NOT NULL')) |
|
58 ] |
|
59 deferred = [ |
|
60 (field.m2m_db_table(), field.m2m_column_name(), opts.db_table, |
|
61 opts.pk.column), |
|
62 (field.m2m_db_table(), field.m2m_reverse_name(), |
|
63 field.rel.to._meta.db_table, field.rel.to._meta.pk.column) |
|
64 ] |
|
65 return table_output, deferred |
|
66 |