From f12b68af71efa0b061710c83364f231734d07e55 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 28 Jun 2014 16:05:13 +0200 Subject: [PATCH] Fixed #16184 -- Fixed multiple PostGIS types introspection Thanks radim.blazek@gmail.com for the report and initial patch. Testing is tricky, as the failure condition is a bit of an edge case. inspectapp.InspectDbTests should at least guarantee non regression. --- .../gis/db/backends/postgis/introspection.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/django/contrib/gis/db/backends/postgis/introspection.py b/django/contrib/gis/db/backends/postgis/introspection.py index 412014bf44..ad8d1db999 100644 --- a/django/contrib/gis/db/backends/postgis/introspection.py +++ b/django/contrib/gis/db/backends/postgis/introspection.py @@ -25,23 +25,25 @@ class PostGISIntrospection(DatabaseIntrospection): identification integers for the PostGIS geometry and/or geography types (if supported). """ - cursor = self.connection.cursor() + field_types = [('geometry', 'GeometryField')] + if self.connection.ops.geography: + # The value for the geography type is actually a tuple + # to pass in the `geography=True` keyword to the field + # definition. + field_types.append(('geography', ('GeometryField', {'geography' : True}))) + postgis_types = {} + # The OID integers associated with the geometry type may # be different across versions; hence, this is why we have # to query the PostgreSQL pg_type table corresponding to the # PostGIS custom data types. oid_sql = 'SELECT "oid" FROM "pg_type" WHERE "typname" = %s' + cursor = self.connection.cursor() try: - cursor.execute(oid_sql, ('geometry',)) - GEOM_TYPE = cursor.fetchone()[0] - postgis_types = {GEOM_TYPE: 'GeometryField'} - if self.connection.ops.geography: - cursor.execute(oid_sql, ('geography',)) - GEOG_TYPE = cursor.fetchone()[0] - # The value for the geography type is actually a tuple - # to pass in the `geography=True` keyword to the field - # definition. - postgis_types[GEOG_TYPE] = ('GeometryField', {'geography': True}) + for field_type in field_types: + cursor.execute(oid_sql, (field_type[0],)) + for result in cursor.fetchall(): + postgis_types[result[0]] = field_type[1] finally: cursor.close()