Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#32721 closed Bug (fixed)

Geometry Index with PostGIS using schema

Reported by: Alan D. Snow Owned by: Alan D. Snow
Component: GIS Version: 3.2
Severity: Normal Keywords:
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 Alan D. Snow)

If you have a model like this:

from django.contrib.gis.db import models as gis_models
from django.db import models

class MyModel(models.Model):
    geometry = gis_models.GeometryField(
        help_text="The area of interest.",
        spatial_index=True,
        srid=4326,
    )
    class Meta:
        db_table = 'source"."mymodel'

When you go to run migrations, this error occurs:

self = <django.db.backends.utils.CursorWrapper object at 0x7f3001dcadc0>
sql = 'CREATE INDEX "source"."mymodel_geometry_id" ON "source"."mymodel" USING GIST ("geometry")'
params = ()
ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0x7f3002ab6790>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7f3001dcadc0>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                # params default might be backend specific.
                return self.cursor.execute(sql)
            else:
>               return self.cursor.execute(sql, params)
E               psycopg2.errors.SyntaxError: syntax error at or near "."
E               LINE 1: CREATE INDEX "source"."mymodel_geometry_id" ON "sour...
E                                            ^

Currently, the code that generates the name is here:
https://github.com/django/django/blob/65a9d0013d202447dd76a9cb3c939aa5c9d23da3/django/contrib/gis/db/backends/postgis/schema.py#L35-L38

        if kwargs.get('name') is None:
            index_name = '%s_%s_id' % (model._meta.db_table, field.column)
        else:
            index_name = kwargs['name']

This produces the index name (as seen in the error above):
source"."mymodel_geometry_id

We have been patching this internally with this code instead:

        name = kwargs.get("name")
        if name is None:
            name = self._create_index_name(
                model._meta.db_table, [field.column], kwargs.get("suffix", "")
            )

This makes the name something like:
mymodel_geometry_26ff3048

Is this a patch you would like to see? Or do you have a different recommended solution?

Change History (9)

comment:1 by Alan D. Snow, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Triage Stage: UnreviewedAccepted

Thanks it looks valid, we should use the _create_index_name() hook instead, e.g. self._create_index_name(model._meta.db_table, [field.column], suffix='_id'). Would you like to prepare a patch? (tests are required).

comment:3 by Alan D. Snow, 4 years ago

Has patch: set
Owner: changed from nobody to Alan D. Snow
Status: newassigned

comment:4 by Alan D. Snow, 4 years ago

Happy to submit a patch. Ref: https://github.com/django/django/pull/14364

comment:5 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:6 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin
Version 0, edited 4 years ago by Mariusz Felisiak (next)

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 99bc67a:

Refs #32721 -- Made PostGISSchemaEditor._create_index_sql() call super()._create_index_sql().

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 29345aec:

Fixed #32721 -- Fixed migrations crash when adding namespaced spatial indexes on PostGIS.

comment:9 by Tim Graham, 3 years ago

The first commit here makes the GIST_GEOMETRY_OPS_ND opclass disappear from the creation of PostGIS's 3D geometry indexes (#33294).

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