Opened 4 weeks ago

Last modified 4 weeks ago

#36694 assigned Bug

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: Accepted
Has patch: no 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 (5)

comment:1 by Luciano de la Iglesia, 4 weeks ago

Description: modified (diff)

comment:2 by Simon Charette, 4 weeks 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 4 weeks ago by Simon Charette (previous) (diff)

comment:3 by Simon Charette, 4 weeks 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, 4 weeks 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, 4 weeks ago

Owner: set to Clifford Gama
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top