1 from django.conf import settings |
|
2 from django.db.backends.creation import BaseDatabaseCreation |
1 from django.db.backends.creation import BaseDatabaseCreation |
3 |
2 |
4 class DatabaseCreation(BaseDatabaseCreation): |
3 class DatabaseCreation(BaseDatabaseCreation): |
5 # This dictionary maps Field objects to their associated PostgreSQL column |
4 # This dictionary maps Field objects to their associated PostgreSQL column |
6 # types, as strings. Column-type strings can contain format strings; they'll |
5 # types, as strings. Column-type strings can contain format strings; they'll |
16 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)', |
15 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)', |
17 'FileField': 'varchar(%(max_length)s)', |
16 'FileField': 'varchar(%(max_length)s)', |
18 'FilePathField': 'varchar(%(max_length)s)', |
17 'FilePathField': 'varchar(%(max_length)s)', |
19 'FloatField': 'double precision', |
18 'FloatField': 'double precision', |
20 'IntegerField': 'integer', |
19 'IntegerField': 'integer', |
|
20 'BigIntegerField': 'bigint', |
21 'IPAddressField': 'inet', |
21 'IPAddressField': 'inet', |
22 'NullBooleanField': 'boolean', |
22 'NullBooleanField': 'boolean', |
23 'OneToOneField': 'integer', |
23 'OneToOneField': 'integer', |
24 'PositiveIntegerField': 'integer CHECK ("%(column)s" >= 0)', |
24 'PositiveIntegerField': 'integer CHECK ("%(column)s" >= 0)', |
25 'PositiveSmallIntegerField': 'smallint CHECK ("%(column)s" >= 0)', |
25 'PositiveSmallIntegerField': 'smallint CHECK ("%(column)s" >= 0)', |
28 'TextField': 'text', |
28 'TextField': 'text', |
29 'TimeField': 'time', |
29 'TimeField': 'time', |
30 } |
30 } |
31 |
31 |
32 def sql_table_creation_suffix(self): |
32 def sql_table_creation_suffix(self): |
33 assert settings.TEST_DATABASE_COLLATION is None, "PostgreSQL does not support collation setting at database creation time." |
33 assert self.connection.settings_dict['TEST_COLLATION'] is None, "PostgreSQL does not support collation setting at database creation time." |
34 if settings.TEST_DATABASE_CHARSET: |
34 if self.connection.settings_dict['TEST_CHARSET']: |
35 return "WITH ENCODING '%s'" % settings.TEST_DATABASE_CHARSET |
35 return "WITH ENCODING '%s'" % self.connection.settings_dict['TEST_CHARSET'] |
36 return '' |
36 return '' |
|
37 |
|
38 def sql_indexes_for_field(self, model, f, style): |
|
39 if f.db_index and not f.unique: |
|
40 qn = self.connection.ops.quote_name |
|
41 db_table = model._meta.db_table |
|
42 tablespace = f.db_tablespace or model._meta.db_tablespace |
|
43 if tablespace: |
|
44 sql = self.connection.ops.tablespace_sql(tablespace) |
|
45 if sql: |
|
46 tablespace_sql = ' ' + sql |
|
47 else: |
|
48 tablespace_sql = '' |
|
49 else: |
|
50 tablespace_sql = '' |
|
51 |
|
52 def get_index_sql(index_name, opclass=''): |
|
53 return (style.SQL_KEYWORD('CREATE INDEX') + ' ' + |
|
54 style.SQL_TABLE(qn(index_name)) + ' ' + |
|
55 style.SQL_KEYWORD('ON') + ' ' + |
|
56 style.SQL_TABLE(qn(db_table)) + ' ' + |
|
57 "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) + |
|
58 "%s;" % tablespace_sql) |
|
59 |
|
60 output = [get_index_sql('%s_%s' % (db_table, f.column))] |
|
61 |
|
62 # Fields with database column types of `varchar` and `text` need |
|
63 # a second index that specifies their operator class, which is |
|
64 # needed when performing correct LIKE queries outside the |
|
65 # C locale. See #12234. |
|
66 db_type = f.db_type() |
|
67 if db_type.startswith('varchar'): |
|
68 output.append(get_index_sql('%s_%s_like' % (db_table, f.column), |
|
69 ' varchar_pattern_ops')) |
|
70 elif db_type.startswith('text'): |
|
71 output.append(get_index_sql('%s_%s_like' % (db_table, f.column), |
|
72 ' text_pattern_ops')) |
|
73 else: |
|
74 output = [] |
|
75 return output |