Opened 14 years ago

Closed 14 years ago

#14922 closed (invalid)

Failure with spatial queries spanning a foreign key join

Reported by: crucialfelix Owned by: nobody
Component: GIS Version: 1.2
Severity: Keywords:
Cc: crucialfelix@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class Apt(Model):

    geo = models.PointField(srid=4326,editable=False,default=Point(0,0))

class AptStatus(Model):
    
    apt = models.ForeignKey(Apt)

from django.contrib.gis.geos import Polygon
box = Polygon.from_bbox(10,20,10,20)
AptStatus.objects.filter(apt__geo__within=box)
FieldError: Join on field 'geo' not permitted. Did you misspell 'within' for the lookup type?

however I've found a nullable foreign key query did work:

class Apt(Model):

    geo = models.PointField(srid=4326,editable=False,default=Point(0,0))
    bldg = models.ForeignKey(Bldg,null=True)

class Bldg(Model):

    geo = models.PointField(srid=4326,editable=False,default=Point(0,0))

Apt.objects.filter(bldg__geo__within=box)

SELECT ...
      FROM "nsproperties_apt"
      INNER JOIN "nsproperties_bldg" ON ("nsproperties_apt"."bldg_id" = "nsproperties_bldg"."id")
      WHERE ST_Within("nsproperties_bldg"."geo",
                      ST_GeomFromEWKB(E'\\001\\003\\000\\000 \\346\\020\\000\\000\\001\\000\\000\\000\\005\\000\\000\\000\\000\\000\\000\\000\\024\\333\\200@\\334\\302Z\\310\\321\\177V\\300\\000\\000\\000\\000\\024\\333\\200@t"\\271,\\272\\177V\\300\\000\\000\\000\\0008\\230\\201@t"\\271,\\272\\177V\\300\\000\\000\\000\\0008\\230\\201@\\334\\302Z\\310\\321\\177V\\300\\000\\000\\000\\000\\024\\333\\200@\\334\\302Z\\310\\321\\177V\\300'))LIMIT 21

Change History (2)

comment:1 by crucialfelix, 14 years ago

postgres

comment:2 by crucialfelix, 14 years ago

Resolution: invalid
Status: newclosed

figured it out: its because a GeoManager is required if any part of the query needs spatial lookups.

I expected that since Apt has a GeoManager that it would be used for queries on its fields.

I guess if the join is happening on another model's fields then the manager for that other model should be used.
otherwise its force me to add a geo manager to a class that has no geo fields, but that's not too bad.

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