#23636: GeoDjango is creating two indexes for geometry field
------------------------------+---------------------------------------
     Reporter:  george-silva  |      Owner:  nobody
         Type:  Bug           |     Status:  new
    Component:  GIS           |    Version:  1.7
     Severity:  Normal        |   Keywords:  geodjango, index, spatial
 Triage Stage:  Unreviewed    |  Has patch:  0
Easy pickings:  1             |      UI/UX:  0
------------------------------+---------------------------------------
 Hello everyone,

 Environment:

 Ubuntu 14.04
 Postgresql 9.3.4
 PostGIS 2.1.3
 Django 1.7

 I'm using a simple model with a custom shapefile importer. Here's the
 model.


 {{{
 class Dsei(models.Model):
     """Modelo que representa um DSEI na base de dados"""

     dsei_siasi = models.OneToOneField(DseiSiasi,
                                       related_name="dsei_geosi",
                                       db_constraint=False)

     geometria = models.PolygonField(verbose_name=u"Geometria",
                                     srid=SRID_PADRAO,
                                     db_index=True,
                                     db_column="geom_geometria")

     def __unicode__(self):
         return "(Geosi) " + self.dsei_siasi.nome
 }}}

 Note that I have a geometric field (geometria), with a specified
 db_index=True.

 I'm using django 1.7 and I noticed that my migration is correct,
 specifying the field. BUT, when I try to import using my custom shapefile
 importer, PostgreSQL complains that the value is to large for the index
 size. When I peeked at the table definition in pgAdmin3, it reported me
 two indexes on the geometric field, a BTREE and a GIST index.


 {{{
 -- Table: aldeias_dsei

 -- DROP TABLE aldeias_dsei;

 CREATE TABLE aldeias_dsei
 (
   id serial NOT NULL,
   geom_geometria geometry(Polygon,4674) NOT NULL,
   dsei_siasi_id numeric(9,0) NOT NULL,
   CONSTRAINT aldeias_dsei_pkey PRIMARY KEY (id),
   CONSTRAINT aldeias_dsei_dsei_siasi_id_key UNIQUE (dsei_siasi_id)
 )
 WITH (
   OIDS=FALSE
 );
 ALTER TABLE aldeias_dsei
   OWNER TO postgres;

 -- Index: aldeias_dsei_f8945348

 -- DROP INDEX aldeias_dsei_f8945348;

 CREATE INDEX aldeias_dsei_f8945348
   ON aldeias_dsei
   USING btree
   (geom_geometria);

 -- Index: aldeias_dsei_geom_geometria_id

 -- DROP INDEX aldeias_dsei_geom_geometria_id;

 CREATE INDEX aldeias_dsei_geom_geometria_id
   ON aldeias_dsei
   USING gist
   (geom_geometria);

 }}}

 My guess is that:

 a) perhaps the migration when applying is creating both indexes
 b) database backend is somehow defaulting to normal behavior AND creating
 the specific index, resulting in two indexes.

 Upon checking the code for sql_indexes_for_field method, looks like B is
 correct. It's calling super.

 I just now saw that GeoDjango has a spatial_index keyword argument, but
 db_index=True is also creating a spatial index.

 This should be fixed, to:

 When you specify a spatial_index=True, only a spatial index should be
 created (this looks correct);
 When you specify a db_index=True, only a BTREE index should be created
 (both indexes are being created, GIST and BTREE);

 My error in code will be solved, I'll switch from db_index to
 spatial_index and it will be solved, but the creation of these both
 indexes should not happen.

--
Ticket URL: <https://code.djangoproject.com/ticket/23636>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/055.7700ec47a66f7f24fb6ad9f1564d9002%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to