Opened 4 hours ago

Last modified 43 seconds ago

#36694 new 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:
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 (4)

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

Description: modified (diff)

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

comment:3 by Simon Charette, 4 minutes 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, 43 seconds 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.

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