﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28353	QuerySet builds wrong SQL when the gis Length function is used and the queryset's query attribute is accessed	Aleksey Ruban	Sergey Fedoseev	"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]"	Bug	closed	GIS	1.11	Normal	fixed	QuerySet gis Length ProgrammingError		Accepted	0	0	0	0	0	0
