Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#13670 closed Bug (fixed)

postgis adapter fails to compare against empty string

Reported by: milosu Owned by: nobody
Component: GIS Version: 1.2
Severity: Normal Keywords:
Cc: milosu, john@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


following test case fails:

from django.db.models import Q
from django.contrib.gis.db import models

class Area(models.Model):
    poly = models.PolygonField(srid=4326, verbose_name=u"area repr")
    objects = models.GeoManager()

class District(models.Model):
    poly = models.PolygonField(srid=4326, verbose_name=u"district repr")
    objects = models.GeoManager()

class Factory(models.Model):
    area = models.ForeignKey(Area)
    district = models.ForeignKey(District)

__test__ = {'API_TESTS':"""

>>> from django.contrib.gis.geos import fromstr
>>> location = fromstr('POINT (5 7)')
>>> a, created = Area.objects.get_or_create(poly=fromstr('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'))
>>> d, created = District.objects.get_or_create(poly=fromstr('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'))
>>> f, created = Factory.objects.get_or_create(area=a, district=d)
>>> areas = Area.objects.filter(poly__intersects = location)
>>> districts = District.objects.filter(poly__intersects = location)
>>> qobj = Q(area__isnull=True)&Q(district__isnull=True)
>>> qobj |= (Q(area__in = areas)|Q(district__in = districts))
>>> match = Factory.objects.filter(qobj)
>>> match2 = Factory.objects.filter(id__in = match)
>>> match2.count()

the last line will raise an exception in django.contrib.gis.db.backends.postgis.adapter eq due to the comparision against empty string

the following patch solves the problem:

---  (revision 3756)
+++  (working copy)
@@ -21,6 +21,8 @@
             raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?')
     def __eq__(self, other):
+        if not isinstance(other, PostGISAdapter):
+           return False
         return (self.ewkb == other.ewkb) and (self.srid == other.srid)
     def __str__(self):

Attachments (1)

13670.1.patch (2.3 KB) - added by John Paulett 8 years ago.
Fix for other backends, move to unittest, & distill test to core example using existing models.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 9 years ago by milosu

Cc: milosu added

comment:2 Changed 9 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:3 Changed 8 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:4 Changed 8 years ago by Julien Phalip

Patch needs improvement: set

Could you provide a single diff patch including the fix and tests? Could you also rewrite your tests using unittests since this is now the preferred way for django's test suite.

Changed 8 years ago by John Paulett

Attachment: 13670.1.patch added

Fix for other backends, move to unittest, & distill test to core example using existing models.

comment:5 Changed 8 years ago by John Paulett

Cc: john@… added
Easy pickings: unset
Patch needs improvement: unset
UI/UX: unset

comment:6 Changed 8 years ago by jbronn

Resolution: fixed
Status: newclosed

In [16757]:

Fixed #13670 -- Comparisons with the spatial adapter won't blow up in some corner cases. Thanks, milosu for the bug report and jpaulett for the patch.

Note: See TracTickets for help on using tickets.
Back to Top