South & GeoDjango & Spatialite

23 views
Skip to first unread message

Douglas Goldstein

unread,
Oct 26, 2014, 1:45:53 AM10/26/14
to south...@googlegroups.com
I hope I've distilled this down to a simple enough example to tell whether I'm doing something wrong or I've uncovered a bug. I am using Django 1.6.6 and South 0.8.4 or 1.0 (tried both).

My model is as follows for the migration below:

class Zone(models.Model):
    name = models.CharField('Zone Name', max_length=1024)
    geo = models.PolygonField('Geo Data about Zone')
    objects = models.GeoManager()


My migration is as follows:

class Migration(SchemaMigration):

    def forwards(self, orm):

        # Adding field 'Zone.geo'
        db.add_column(u'mapping_zone', 'geo',
                      self.gf('django.contrib.gis.db.models.fields.PolygonField')(null=True),
                      keep_default=False)


If I run this migration as: ./manage.py migrate -v 3 mapping
I get the following output:

 > mapping:0002_auto__chg_field_zone_geo
   = SELECT AddGeometryColumn('mapping_zone', 'geo', 4326, 'POLYGON', 2, 0); []
   = SELECT CreateSpatialIndex('mapping_zone', 'geo'); []
   = CREATE TABLE "_south_new_mapping_zone" ("geo" None, "map_id" integer NOT NULL, "name" varchar(1024) NOT NULL) []
   = INSERT INTO "_south_new_mapping_zone" ("name", "map_id") SELECT "name", "map_id", FROM "mapping_zone"; []
   = DROP TABLE "mapping_zone"; []
   = ALTER TABLE "_south_new_mapping_zone" RENAME TO "mapping_zone"; []

   = CREATE INDEX "mapping_zone_51513134" ON "mapping_zone"("map_id"); []

The result is the new mapping_zone table is created incorrectly and doesn't have the field type set correctly and all the Spatialite fields are missing. It appears that I need to suppress the CREATE TABLE from occurring. The hack to make this happens appears to be to edit south/db/sqlite3.py in the add_column() method, the last line is a call to self._remake_table() which needs to be changed as follows:

        if field.__class__.__module__ == 'django.contrib.gis.db.models.fields':
            self._column_sql_for_create(table_name, name, field, False)
        else:
            self._remake_table(table_name, added={
                field.column: (self._column_sql_for_create(table_name, name, field, False), field_default)
            })


Now if I delete my database and start over and migrate again I get the following behavior:

 > mapping:0002_auto__chg_field_zone_geo
   = SELECT AddGeometryColumn('mapping_zone', 'geo', 4326, 'POLYGON', 2, 0); []
   = SELECT CreateSpatialIndex('mapping_zone', 'geo'); []
   = SELECT AddGeometryColumn('mapping_zone', 'geo', 4326, 'POLYGON', 2, 0); []
   = SELECT CreateSpatialIndex('mapping_zone', 'geo'); []


The double call doesn't seem to harm anything but not sure how to suppress it.

Any advice would be much appreciated.

Andrew Godwin

unread,
Oct 26, 2014, 4:25:25 AM10/26/14
to south...@googlegroups.com
Hi Douglas,

South does not have very good support for Spatialite at all; this is mostly likely a bug in South, but one that will (unfortunately) never be fixed, as South has been EOL'd.

Django 1.7 has better (i.e. actually tested) support for Spatialite and a whole lot of workarounds to make things sort of work - remember that Spatialite, much like SQLite, does not really support table mutation very much (though add column is one of the few things it can do natively). I would encourage you to use PostGIS unless you've got a real good reason for using Spatialite (i.e. you're doing embedded development).

Andrew

--
You received this message because you are subscribed to the Google Groups "South Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to south-users...@googlegroups.com.
To post to this group, send email to south...@googlegroups.com.
Visit this group at http://groups.google.com/group/south-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages