|
0
|
1 |
from django.db.backends.creation import BaseDatabaseCreation |
|
|
2 |
|
|
|
3 |
class DatabaseCreation(BaseDatabaseCreation): |
|
|
4 |
# This dictionary maps Field objects to their associated PostgreSQL column |
|
|
5 |
# types, as strings. Column-type strings can contain format strings; they'll |
|
|
6 |
# be interpolated against the values of Field.__dict__ before being output. |
|
|
7 |
# If a column type is set to None, it won't be included in the output. |
|
|
8 |
data_types = { |
|
|
9 |
'AutoField': 'serial', |
|
|
10 |
'BooleanField': 'boolean', |
|
|
11 |
'CharField': 'varchar(%(max_length)s)', |
|
|
12 |
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)', |
|
|
13 |
'DateField': 'date', |
|
|
14 |
'DateTimeField': 'timestamp with time zone', |
|
|
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', |
|
29
|
20 |
'BigIntegerField': 'bigint', |
|
0
|
21 |
'IPAddressField': 'inet', |
|
|
22 |
'NullBooleanField': 'boolean', |
|
|
23 |
'OneToOneField': 'integer', |
|
|
24 |
'PositiveIntegerField': 'integer CHECK ("%(column)s" >= 0)', |
|
|
25 |
'PositiveSmallIntegerField': 'smallint CHECK ("%(column)s" >= 0)', |
|
|
26 |
'SlugField': 'varchar(%(max_length)s)', |
|
|
27 |
'SmallIntegerField': 'smallint', |
|
|
28 |
'TextField': 'text', |
|
|
29 |
'TimeField': 'time', |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
def sql_table_creation_suffix(self): |
|
29
|
33 |
assert self.connection.settings_dict['TEST_COLLATION'] is None, "PostgreSQL does not support collation setting at database creation time." |
|
|
34 |
if self.connection.settings_dict['TEST_CHARSET']: |
|
|
35 |
return "WITH ENCODING '%s'" % self.connection.settings_dict['TEST_CHARSET'] |
|
0
|
36 |
return '' |
|
29
|
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 |