[Django] #28353: QuerySet builds wrong SQL when the gis Length function is used

6 views
Skip to first unread message

Django

unread,
Jun 30, 2017, 12:43:05 PM6/30/17
to django-...@googlegroups.com
#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.

Django

unread,
Jun 30, 2017, 1:55:56 PM6/30/17
to django-...@googlegroups.com
#28353: QuerySet builds wrong SQL when the gis Length function is used and the
queryset's query attribute is accessed
-------------------------------------+-------------------------------------
Reporter: Aleksey Ruban | Owner: nobody
Type: Bug | Status: new
Component: GIS | Version: 1.11
Severity: Normal | Resolution:
Keywords: QuerySet gis Length | Triage Stage: Accepted
ProgrammingError |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted
* component: Database layer (models, ORM) => GIS


--
Ticket URL: <https://code.djangoproject.com/ticket/28353#comment:1>

Django

unread,
Jun 30, 2017, 2:07:55 PM6/30/17
to django-...@googlegroups.com
#28353: QuerySet builds wrong SQL when the gis Length function is used and the
queryset's query attribute is accessed
-------------------------------------+-------------------------------------
Reporter: Aleksey Ruban | Owner: Sergey
| Fedoseev
Type: Bug | Status: assigned

Component: GIS | Version: 1.11
Severity: Normal | Resolution:
Keywords: QuerySet gis Length | Triage Stage: Accepted
ProgrammingError |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* owner: nobody => Sergey Fedoseev
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28353#comment:2>

Django

unread,
Jul 5, 2017, 7:38:50 AM7/5/17
to django-...@googlegroups.com
#28353: QuerySet builds wrong SQL when the gis Length function is used and the
queryset's query attribute is accessed
-------------------------------------+-------------------------------------
Reporter: Aleksey Ruban | Owner: Sergey
| Fedoseev
Type: Bug | Status: assigned
Component: GIS | Version: 1.11
Severity: Normal | Resolution:
Keywords: QuerySet gis Length | Triage Stage: Accepted
ProgrammingError |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Sergey Fedoseev):

[https://github.com/django/django/pull/8705 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/28353#comment:3>

Django

unread,
Sep 11, 2017, 11:57:10 AM9/11/17
to django-...@googlegroups.com
#28353: QuerySet builds wrong SQL when the gis Length function is used and the
queryset's query attribute is accessed
-------------------------------------+-------------------------------------
Reporter: Aleksey Ruban | Owner: Sergey
| Fedoseev
Type: Bug | Status: closed
Component: GIS | Version: 1.11
Severity: Normal | Resolution: fixed

Keywords: QuerySet gis Length | Triage Stage: Accepted
ProgrammingError |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"3905cfa1a578275323bfbfbef09f5aee05b33301" 3905cfa1]:
{{{
#!CommitTicketReference repository=""
revision="3905cfa1a578275323bfbfbef09f5aee05b33301"
Fixed #28353 -- Fixed some GIS functions when queryset is evaluated more
than once.

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

--
Ticket URL: <https://code.djangoproject.com/ticket/28353#comment:4>

Reply all
Reply to author
Forward
0 new messages