Opened 5 months ago

Closed 2 months ago

#28353 closed Bug (fixed)

QuerySet builds wrong SQL when the gis Length function is used and the queryset's query attribute is accessed

Reported by: Aleksey Ruban Owned by: Sergey Fedoseev
Component: GIS Version: 1.11
Severity: Normal Keywords: QuerySet gis Length ProgrammingError
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (4)

comment:1 Changed 5 months ago by Tim Graham

Component: Database layer (models, ORM)GIS
Summary: QuerySet builds wrong SQL when the gis Length function is usedQuerySet builds wrong SQL when the gis Length function is used and the queryset's query attribute is accessed
Triage Stage: UnreviewedAccepted

comment:2 Changed 5 months ago by Sergey Fedoseev

Owner: changed from nobody to Sergey Fedoseev
Status: newassigned

comment:3 Changed 5 months ago by Sergey Fedoseev

comment:4 Changed 2 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 3905cfa1:

Fixed #28353 -- Fixed some GIS functions when queryset is evaluated more than once.

Reverted test for refs #27603 in favor of using FuncTestMixin.

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