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.
This commit is contained in:
Claude Paroz 2014-06-28 16:05:13 +02:00
parent 70576740b0
commit f12b68af71
1 changed files with 13 additions and 11 deletions

View File

@ -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()