1 # This dictionary maps Field objects to their associated MySQL column |
1 from django.conf import settings |
2 # types, as strings. Column-type strings can contain format strings; they'll |
2 from django.db.backends.creation import BaseDatabaseCreation |
3 # be interpolated against the values of Field.__dict__ before being output. |
3 |
4 # If a column type is set to None, it won't be included in the output. |
4 class DatabaseCreation(BaseDatabaseCreation): |
5 DATA_TYPES = { |
5 # This dictionary maps Field objects to their associated MySQL column |
6 'AutoField': 'integer AUTO_INCREMENT', |
6 # types, as strings. Column-type strings can contain format strings; they'll |
7 'BooleanField': 'bool', |
7 # be interpolated against the values of Field.__dict__ before being output. |
8 'CharField': 'varchar(%(max_length)s)', |
8 # If a column type is set to None, it won't be included in the output. |
9 'CommaSeparatedIntegerField': 'varchar(%(max_length)s)', |
9 data_types = { |
10 'DateField': 'date', |
10 'AutoField': 'integer AUTO_INCREMENT', |
11 'DateTimeField': 'datetime', |
11 'BooleanField': 'bool', |
12 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)', |
12 'CharField': 'varchar(%(max_length)s)', |
13 'FileField': 'varchar(%(max_length)s)', |
13 'CommaSeparatedIntegerField': 'varchar(%(max_length)s)', |
14 'FilePathField': 'varchar(%(max_length)s)', |
14 'DateField': 'date', |
15 'FloatField': 'double precision', |
15 'DateTimeField': 'datetime', |
16 'ImageField': 'varchar(%(max_length)s)', |
16 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)', |
17 'IntegerField': 'integer', |
17 'FileField': 'varchar(%(max_length)s)', |
18 'IPAddressField': 'char(15)', |
18 'FilePathField': 'varchar(%(max_length)s)', |
19 'NullBooleanField': 'bool', |
19 'FloatField': 'double precision', |
20 'OneToOneField': 'integer', |
20 'IntegerField': 'integer', |
21 'PhoneNumberField': 'varchar(20)', |
21 'IPAddressField': 'char(15)', |
22 'PositiveIntegerField': 'integer UNSIGNED', |
22 'NullBooleanField': 'bool', |
23 'PositiveSmallIntegerField': 'smallint UNSIGNED', |
23 'OneToOneField': 'integer', |
24 'SlugField': 'varchar(%(max_length)s)', |
24 'PositiveIntegerField': 'integer UNSIGNED', |
25 'SmallIntegerField': 'smallint', |
25 'PositiveSmallIntegerField': 'smallint UNSIGNED', |
26 'TextField': 'longtext', |
26 'SlugField': 'varchar(%(max_length)s)', |
27 'TimeField': 'time', |
27 'SmallIntegerField': 'smallint', |
28 'USStateField': 'varchar(2)', |
28 'TextField': 'longtext', |
29 } |
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 |