[Django] #24222: UnboundLocalError: local variable 'sql' referenced before assignment

9 views
Skip to first unread message

Django

unread,
Jan 26, 2015, 8:37:07 AM1/26/15
to django-...@googlegroups.com
#24222: UnboundLocalError: local variable 'sql' referenced before assignment
----------------------------------------------+--------------------
Reporter: oesgalha | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I have a model similar to this one:
{{{
class Sale(models.Model):
price = models.DecimalField(max_digits=8, decimal_places=2)
delivery_date = models.DateTimeField()
}}}
And I want to get the sum of sales per month, so I tried this:
{{{
models.Sale.objects.datetimes('delivery_date',
'month').annotate(Sum('price'))
}}}

Since datetimes returns a new QuerySet, I expected that code would work,
but I stumbled on this error message:
{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/query.py", line 116, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/query.py", line 141, in __iter__
self._fetch_all()
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/query.py", line 966, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 1130, in results_iter
for rows in self.execute_sql(MULTI):
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 775, in execute_sql
sql, params = self.as_sql()
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 133, in as_sql
grouping, gb_params = self.get_grouping(having_group_by,
ordering_group_by)
File "/home/vagrant/.virtualenvs/awesomeapp/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 586, in get_grouping
if sql not in seen:
UnboundLocalError: local variable 'sql' referenced before assignment
}}}

So I took a look at the source code:
https://github.com/django/django/blob/1.7.3/django/db/models/sql/compiler.py#L586

And if I read it correctly: if the condition in line 582 is sufficed, then
the var 'sql' is indeed unassigned in the line 586.


I'm using python 2.7.6 and Django 1.7.3
The database is MySQL 5.5

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

Django

unread,
Jan 31, 2015, 4:51:08 PM1/31/15
to django-...@googlegroups.com
#24222: UnboundLocalError: local variable 'sql' referenced before assignment
-------------------------------------+-------------------------------------

Reporter: oesgalha | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
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
-------------------------------------+-------------------------------------
Changes (by inglesp):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

This is fixed in 1.8, and was fixed by
[https://github.com/django/django/commit/cbb5cdd this commit].

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

Django

unread,
Jan 31, 2015, 6:46:28 PM1/31/15
to django-...@googlegroups.com
#24222: UnboundLocalError: local variable 'sql' referenced before assignment
-------------------------------------+-------------------------------------
Reporter: oesgalha | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

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


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

Reply all
Reply to author
Forward
0 new messages