1
from django.conf import settings
2
from django.db.backends.postgresql.creation import DatabaseCreation
4
class PostGISCreation(DatabaseCreation):
5
geom_index_type = 'GIST'
6
geom_index_opts = 'GIST_GEOMETRY_OPS'
8
def sql_indexes_for_field(self, model, f, style):
9
"Return any spatial index creation SQL for the field."
10
from django.contrib.gis.db.models.fields import GeometryField
12
output = super(PostGISCreation, self).sql_indexes_for_field(model, f, style)
14
if isinstance(f, GeometryField):
15
gqn = self.connection.ops.geo_quote_name
16
qn = self.connection.ops.quote_name
17
db_table = model._meta.db_table
20
# Geogrophy columns are created normally.
23
# Geometry columns are created by `AddGeometryColumn`
25
output.append(style.SQL_KEYWORD('SELECT ') +
26
style.SQL_TABLE('AddGeometryColumn') + '(' +
27
style.SQL_TABLE(gqn(db_table)) + ', ' +
28
style.SQL_FIELD(gqn(f.column)) + ', ' +
29
style.SQL_FIELD(str(f.srid)) + ', ' +
30
style.SQL_COLTYPE(gqn(f.geom_type)) + ', ' +
31
style.SQL_KEYWORD(str(f.dim)) + ');')
34
# Add a NOT NULL constraint to the field
35
output.append(style.SQL_KEYWORD('ALTER TABLE ') +
36
style.SQL_TABLE(qn(db_table)) +
37
style.SQL_KEYWORD(' ALTER ') +
38
style.SQL_FIELD(qn(f.column)) +
39
style.SQL_KEYWORD(' SET NOT NULL') + ';')
43
# Spatial indexes created the same way for both Geometry and
48
index_opts = ' ' + style.SQL_KEYWORD(self.geom_index_opts)
49
output.append(style.SQL_KEYWORD('CREATE INDEX ') +
50
style.SQL_TABLE(qn('%s_%s_id' % (db_table, f.column))) +
51
style.SQL_KEYWORD(' ON ') +
52
style.SQL_TABLE(qn(db_table)) +
53
style.SQL_KEYWORD(' USING ') +
54
style.SQL_COLTYPE(self.geom_index_type) + ' ( ' +
55
style.SQL_FIELD(qn(f.column)) + index_opts + ' );')
58
def sql_table_creation_suffix(self):
59
qn = self.connection.ops.quote_name
60
return ' TEMPLATE %s' % qn(getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis'))