#28353: QuerySet builds wrong SQL when the gis Length function is used
-------------------------------------+-------------------------------------
               Reporter:  Aleksey    |          Owner:  nobody
  Ruban                              |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.11
  layer (models, ORM)                |       Keywords:  QuerySet gis Length
               Severity:  Normal     |  ProgrammingError
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I get ProgrammingError when trying to use the Length function from gis
 package. For this reason I can't use it with django-rest-framework. It
 throws the error when I try to get a list. This bug emerges in Django
 1.10, 1.11. I don't try to test it against other versions.
 \\

 {{{
 Traceback (most recent call last):
   File "/media/sf_django/django_gis_bug/test_gis_bug/tests.py", line 16,
 in test_gis_bug
     features.first()
   File "/home/alex/.virtualenvs/django_test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 564, in first
     objects = list((self if self.ordered else self.order_by('pk'))[:1])
   File "/home/alex/.virtualenvs/django_test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 250, in __iter__
     self._fetch_all()
   File "/home/alex/.virtualenvs/django_test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 1105, in _fetch_all
     self._result_cache = list(self._iterable_class(self))
   File "/home/alex/.virtualenvs/django_test/local/lib/python2.7/site-
 packages/django/db/models/query.py", line 53, in __iter__
     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
   File "/home/alex/.virtualenvs/django_test/local/lib/python2.7/site-
 packages/django/db/models/sql/compiler.py", line 886, in execute_sql
     raise original_exception
 ProgrammingError: function st_lengthspheroid(geometry, unknown, unknown)
 does not exist
 LINE 1: ...ug_feature"."id", "test_gis_bug_feature"."shape", ST_LengthS...
                                                              ^
 HINT:  No function matches the given name and argument types. You might
 need to add explicit type casts.
 }}}
 \\
 It happens on access to the query attribute of the QuerySet class. On
 every access an additional parameter gets added to the st_lengthspheroid
 function. \\
 models.py
 {{{
 from django.contrib.gis.db import models


 class Feature(models.Model):
     shape = models.LineStringField()
 }}}
 \\
 to test

 {{{
 from django.contrib.gis.db.models.functions import Length

 from .models import Feature


 features = Feature.objects.annotate(length=Length('shape'))
 # This action adds an additional parameter to the st_lengthspheroid
 # function every time you call the str function on the queryset
 str(features.query)
 # As a result it raises ProgrammingError, but it must pass without an
 error.
 features.first()
 }}}
 I created an app with a test case:
 [https://github.com/rafick1983/django_gis_bug]

--
Ticket URL: <https://code.djangoproject.com/ticket/28353>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.6a381500341db0ab625537685eb50048%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to