Opened 10 months ago

Closed 3 weeks ago

Last modified 3 weeks ago

#36694 closed Bug (fixed)

PostGIS backend silently ignores most Index options when targeting a single GeometryField (expressions, condition, include, opclasses)

Reported by: Luciano de la Iglesia Owned by: Clifford Gama
Component: GIS Version: 5.2
Severity: Normal Keywords: postgis index condition include
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Luciano de la Iglesia)

I have this model:

class Foo(models.Model):
    location = geomodels.PointField(geography=True, spatial_index=False)
    is_current = models.BooleanField()

    class Meta:
        indexes = [
            GistIndex(fields=["location"], condition=Q(is_current=True), name="current_foo"),
        ]

When I create the migration, the sql for my current_foo index is:

CREATE INDEX "current_foo" ON "webatds_foo" USING GIST ("location");

This ignores my condition, I expect it to be:

CREATE INDEX "current_foo" ON "webatds_foo" USING GIST ("location") where is_current=true;

I'm using django 5.2.7, psycopg 3.2.12, and PostGIS 17.3.5.

Change History (11)

comment:1 by Luciano de la Iglesia, 10 months ago

Description: modified (diff)

comment:2 by Simon Charette, 10 months ago

Component: MigrationsGIS
Keywords: postgis index condition include added
Summary: GistIndex with condition ignores conditionPostGIS backend silently ignores most Index options (expressions, condition, include, opclasses)
Triage Stage: UnreviewedAccepted

Confirmed this is a bug in in django.contrib.gis.db.backends.postgist (I inferred you were using PostGIS given you use contrib.postgres.ArrayField).

The SchemaEditor._create_index_sql method delegates to ._create_spatial_index_sql to provide some defaults (e.g. opclasses) but fails to pass along kwargs such as condition, include, expressions, etc, when calling super().

It seems broken in multiple ways.

Last edited 10 months ago by Simon Charette (previous) (diff)

comment:3 by Simon Charette, 10 months ago

Summary: PostGIS backend silently ignores most Index options (expressions, condition, include, opclasses)PostGIS backend silently ignores most Index options when targeting a single GeometryField (expressions, condition, include, opclasses)

comment:4 by Simon Charette, 10 months ago

A workaround in the meantime could be to use expressions=[F("location")] instead of fields=["location"]. You might have to explicitly specify the proper opclasses though, that would be point_ops in your case.

comment:5 by Clifford Gama, 10 months ago

Owner: set to Clifford Gama
Status: newassigned

comment:6 by Clifford Gama, 7 months ago

Has patch: set

comment:7 by David Smith, 6 weeks ago

Patch needs improvement: set

comment:8 by Clifford Gama, 4 weeks ago

Patch needs improvement: unset

comment:9 by Jacob Walls, 3 weeks ago

Triage Stage: AcceptedReady for checkin

comment:10 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

Resolution: fixed
Status: assignedclosed

In 76e1bca1:

Fixed #36694 -- Restored missing Index options in PostGISSchemaEditor._create_spatial_index_sql().

Thanks Luciano de la Iglesia for the report, and Simon Charette and David Smith
for the reviews.

comment:11 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

In 50c2b7c:

Refs #36694 -- Skipped GIS test needing spatial index on Oracle.

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