#32721: Geometry Index with PostGIS using schema
------------------------------+--------------------------------------
     Reporter:  Alan D. Snow  |                    Owner:  nobody
         Type:  Bug           |                   Status:  new
    Component:  GIS           |                  Version:  3.2
     Severity:  Normal        |               Resolution:
     Keywords:                |             Triage Stage:  Unreviewed
    Has patch:  0             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+--------------------------------------
Description changed by Alan D. Snow:

Old description:

> 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(
>                 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?

New description:

 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?

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32721#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.e126ea9c81e3cbc80b8cfa92e7575eb2%40djangoproject.com.

Reply via email to